2

I am trying to code an XOR Gate and I found this:

return in[0] != in[1];

where in[0] is for example true and in[1] is false. I understand that ! gives the negation but why is = used?

4

4 回答 4

4

Consider the truth table:

0   1   XOR
0   0   0
1   0   1
0   1   1
1   1   0

The example works because 0 is equal to false and 1 is equal to true In both cases, 0 != 0 is false = 0, since 0 does equal 0. You can work out all other

于 2014-01-02T18:29:12.553 回答
2

!= is just the character sequence for the "not equal" operator. xor is actually ^ operator.

于 2014-01-02T18:28:33.800 回答
2

运算符的!=意思不等于。

于 2014-01-02T18:25:49.757 回答
0

您可以使用 OR 和 AND 运算符执行XOR运算符。例如:

return (in[0] | in[1]) & !(in[0] & in[1]);

于 2014-01-02T18:50:23.927 回答