Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XOR operation on three values

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
like image 455
Hatefiend Avatar asked Aug 31 '18 05:08

Hatefiend


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.

How does XOR work with multiple inputs?

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.


2 Answers

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 .

see image 1

For the second expression AB' .

see image 2

For the third expression B'C .

see image 3

For the fourth expression BC' .

enter image description here

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)
like image 171
Leandro Keen Zapa Avatar answered Sep 30 '22 10:09

Leandro Keen Zapa


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)

like image 33
subdeveloper Avatar answered Sep 30 '22 10:09

subdeveloper