Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xor with 3 values

I need to do an xor conditional between 3 values, ie i need one of the three values to be true but not more than one and not none.

I thought i could use the xor ^ operator for this but its not working as expected.

I expected that this would return false but it doesnt. (true ^ true ^ true)

all other combinations seem to work as i expected.

When looking at the docs for the xor operator they only talk about comparing 2 values and i cant find anything on doing this for 3 or more values online.

Can anyone shed any light or suggest a simple way of doing this?

like image 823
sianabanana Avatar asked Jun 03 '11 14:06

sianabanana


People also ask

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

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.


1 Answers

One way would be to convert the Boolean values to an integer, add the results, and compare to 1.

like image 129
jcomeau_ictx Avatar answered Sep 19 '22 22:09

jcomeau_ictx