I have three boolean values. I need to return false
if all three are true
or if all three are false
. I will return true
in every other situation. Based on my research, in some specifications this is called a three-variable exclusive-or.
Edit: Some specifications claim a three variable XOR involves the only true
result would come from a set where only one parameter is true
. The XOR I am referring to here is of another specification where multiple values may be true
, but not all.
What's the fastest way to perform this operation? a xor b xor c
doesn't work
What's if it wasn't three but n parameters?
Here's the truth table for my desired operation (xor with three params).
A B C -
T T T F
T T F T
T F T T
T F F T
F T T T
F T F T
F F T T
F F F F
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.
If both of an XOR gate's inputs are false, or if both of its inputs are true, then the output of the XOR gate is false. If an XOR gate has more than two inputs, then its behavior depends on its implementation. In the vast majority of cases, an XOR gate will output true if an odd number of its inputs is true.
To make an algorithm for that, you need to know how to use karnaugh map in three variable. See sample karnaugh map here
Ok. First, to make things easier replace T as 1 and F as 0 in your truth table.
At first glance, it is just an increasing 3-bit binary. So arranging it in an increasing way is a good idea. Take a look below.
A B C F(A,B,C)
0 0 0 0
0 0 1 1
0 1 0 1
0 1 1 1
1 0 0 1
1 0 1 1
1 1 0 1
1 1 1 0
By using karnaugh-map, you will get a boolean expression below. For the first expression we get A'B .
For the second expression AB' .
For the third expression B'C .
For the fourth expression BC' .
To simply understand karnaugh-map, if all 1's are inside the straight sight towards the table of a variable then one term of expression will contain only that variable. But if 1's are outside the straight sight of that variable, then, it is a compliment of that variable.
F(A,B,C) = A'B + AB'+ B'C + BC'
but since
A XOR B = AB'+ A'B
B XOR C = BC'+ B'C
then our simplified form will be
F(A,B,C) = A XOR B + B XOR C
for pseudo code programming, it is equivalent to
result = (A XOR B) OR (B XOR C)
//other else
result = (A ^ B) | (B ^ C)
Use this
(A xor B) or (B xor C)
Works for n
inputs as well.
(A xor B) or (B xor C) ... or (n xor n+1)
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