2

I've got a program that compares 2 boolean values, say, conditionX, and conditionY.

Both are initially set to false, but after a series of switches at least one of them becomes true.

I need to test both conditionX and conditionY. If either one of them comes out as true, then the test returns true, but if both of them return false, the test returns false.

And here's the part I need help with. If both of them return true, the test MUST return FALSE.

And, here, I'm drawing a blank. I know the AND operator will only return true, if both are true, while the OR operator will return true if at least one of them returns true.

Is there an operator that will return false if both of them return true/false, but will return true if at least one of them is true?

7 Answers 7

8

Try XOR

if(statement1 ^ statement2)
    doSomething();
Sign up to request clarification or add additional context in comments.

Comments

7

Use an xor

conditionX ^conditionY

Comments

5

You can Use the ^ operator.

http://msdn.microsoft.com/en-us/library/zkacc7k1%28v=VS.100%29.aspx

4 Comments

Why the -1 (+1 because I think it's bizare)
@Rune, I didn't DV, but the operator is not forward-slash back-slash. It is the caret: ^
Its my mistake. Actually i mean caret. Sorry guys. But my link points to correct one.
Yeah, I fixed the post. It was obvious what you meant, even if you didn't use the right character.
5

Use the preferred one from below. They are listed as per my preference.

conditionX ^ conditionY
// OR
conditionX != conditionY
// OR
(conditionX || conditionY) && !(conditionX && conditionY)

Comments

3

Try this: conditionX != conditionY.

Comments

0

if you know that true==1 and false==0, then you can appily a bitwise xor ^. otherwise simply use

(a||b)&&(!a||!b)

1 Comment

In C#, the ^ works for boolean without worrying about what "true" or "false" represents numerically.
0

XOR

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.