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 re开发者_高级运维turns 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?
Try XOR
if(statement1 ^ statement2)
doSomething();
Use an xor
conditionX ^conditionY
You can Use the ^
operator.
http://msdn.microsoft.com/en-us/library/zkacc7k1%28v=VS.100%29.aspx
Use the preferred one from below. They are listed as per my preference.
conditionX ^ conditionY
// OR
conditionX != conditionY
// OR
(conditionX || conditionY) && !(conditionX && conditionY)
Try this: conditionX != conditionY
.
if you know that true==1 and false==0, then you can appily a bitwise xor ^
. otherwise simply use
(a||b)&&(!a||!b)
XOR
精彩评论