zack_falcon
zack_falcon

Reputation: 4386

I need help with C# operators

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?

Upvotes: 2

Views: 181

Answers (7)

CharithJ
CharithJ

Reputation: 47600

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

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

Upvotes: 5

Rune FS
Rune FS

Reputation: 21752

Use an xor

conditionX ^conditionY

Upvotes: 7

Jeyara
Jeyara

Reputation: 2298

You can Use the ^ operator.

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

Upvotes: 5

trailmax
trailmax

Reputation: 35126

XOR

Upvotes: 0

MRAB
MRAB

Reputation: 20664

Try this: conditionX != conditionY.

Upvotes: 3

anthony sottile
anthony sottile

Reputation: 70233

Try XOR

if(statement1 ^ statement2)
    doSomething();

Upvotes: 8

Dave
Dave

Reputation: 11162

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

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

Upvotes: 0

Related Questions