What is the simplest way to do a three-way exclusive OR?
In other words, I have three values, and I want a statement that evaluates to true IFF only one of the three values is true.
So far, this is what I've come up with:
((a ^ b) && (a ^ c) && !(b && c)) || ((b ^ a) && (b ^ c) && !(a && c)) || ((c ^ a) && (c ^ b) && !(a && b))
Is there something simpler to do the same thing?
Here's the proof that the above accomplishes the task:
a = true; b = true; c = true ((a ^ b) && (a ^ c) && !(b && c)) || ((b ^ a) && (b ^ c) && !(a && c)) || ((c ^ a) && (c ^ b) && !(a && b)) => false a = true; b = true; c = false ((a ^ b) && (a ^ c) && !(b && c)) || ((b ^ a) && (b ^ c) && !(a && c)) || ((c ^ a) && (c ^ b) && !(a && b)) => false a = true; b = false; c = true ((a ^ b) && (a ^ c) && !(b && c)) || ((b ^ a) && (b ^ c) && !(a && c)) || ((c ^ a) && (c ^ b) && !(a && b)) => false a = true; b = false; c = false ((a ^ b) && (a ^ c) && !(b && c)) || ((b ^ a) && (b ^ c) && !(a && c)) || ((c ^ a) && (c ^ b) && !(a && b)) => true a = false; b = true; c = true ((a ^ b) && (a ^ c) && !(b && c)) || ((b ^ a) && (b ^ c) && !(a && c)) || ((c ^ a) && (c ^ b) && !(a && b)) => false a = false; b = true; c = false ((a ^ b) && (a ^ c) && !(b && c)) || ((b ^ a) && (b ^ c) && !(a && c)) || ((c ^ a) && (c ^ b) && !(a && b)) => true a = false; b = false; c = true ((a ^ b) && (a ^ c) && !(b && c)) || ((b ^ a) && (b ^ c) && !(a && c)) || ((c ^ a) && (c ^ b) && !(a && b)) => true a = false; b = false; c = false ((a ^ b) && (a ^ c) && !(b && c)) || ((b ^ a) && (b ^ c) && !(a && c)) || ((c ^ a) && (c ^ b) && !(a && b)) => false
For 3 or more inputs, the XOR gate has a value of 1when there is an odd number of 1's in the inputs, otherwise, it is a 0. Notice also that the truth tables for the 3-input XOR and XNOR gates are identical.
For 3-input XOR gates, we can have the HIGH input when odd numbers of inputs are at HIGH level. So the 3-input OR gate is called as “Odd functioned OR gate”.
For exactly three terms, you can use this expression:
(a ^ b ^ c) && !(a && b && c)
The first part is true
iff one or three of the terms are true
. The second part of the expression ensures that not all three are true
.
Note that the above expression does NOT generalize to more terms. A more general solution is to actually count how many terms are true
, so something like this:
int trueCount = (a ? 1 : 0) + (b ? 1 : 0) + (c ? 1 : 0) + ... // more terms as necessary return (trueCount == 1); // or some range check expression etc
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