Given two Boolean, how to come up with the most elegant one liner that computes the XOR operation in C#?
I know one can do this by a combination of switch
or if
else
but that would make my code rather ugly.
The XOR logical operation, exclusive or, takes two boolean operands and returns true if, and only if, the operands are different. Conversely, it returns false if the two operands have the same value.
XOR Boolean Expression It can also be written as: (A + B) (A + B)
To find XOR of more than two numbers, represent all numbers in binary representation, add 0's before if necessary. Write them like this. and so on. To find each bit of XOR just calculate number of 1's in the corresponding bits.
Examples: 1 XOR 1 = 0. 1 XOR 0 = 1. 0 XOR 1 = 1.
bool xorValue = bool1 ^ bool2;
Ok to add some context: You can look here Tables
There you can see that "exclusive or" is basically the same as "not equal". So you could just use this (with boolean):
if (X != Y)...
But if you want to directly show people you mean "XOR" just use the other answers here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With