Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XOR of three values

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 
like image 690
Josh Avatar asked Aug 12 '10 09:08

Josh


People also ask

How does XOR work with 3 variables?

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.

Can XOR have 3 inputs?

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”.


1 Answers

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 
like image 55
polygenelubricants Avatar answered Oct 14 '22 14:10

polygenelubricants