While many higher level languages have bitwise (exclusive or) and bitwise (exclusive and), for instance C, C++, Java, etc. I'm curious why the ( vastly more useful ) logical short-circuit operators don't have this functionality? Is it simply due to the capacity of being able to concatenate logic through use of "not"?
This question is mainly to do with the language design considerations….
Best Answer
Binary
Nand
Lets look at how NAND can be implemented with just AND or OR gates.
NAND becomes one of:
Either of these can be seen as short circuiting. The reason that NAND doesn't exist is that it is easily rewritten as
not(a and b)
Xor
XOR, is at its heart, a parity checker. To check the parity of two values, you need to test both values. This is why it is fundamentally not able to short circuit it - you can't validate if the value is true or false until you test all the values.
Looking at how XOR is written with AND and OR gates:
If
a
is true, the OR part of the xor can be short circuited, however it also means that the AND part cannot be short circuited.N-ary
N-ary operands take any number of inputs (compared to the binary ones that just take two).
Nand
The n-ary NAND is
This again can be short circuited in that as soon as one of operands evaluates to false, the value of the NAND is true.
Xor
There are two different interpretations of the n-ary xor:
The first one is in common usage (see XOR at Wolfram):
From Wikipedia
The 'one hot' xor may be short circuited when evaluating the expression when finding the second true value.
Otherwise, again, the XOR is more commonly implemented as the "odd number of true values" which serves as a parity checker and requires the evaluation of all the operands to determine the truth of the expression as the last value evaluated can always change the truth of the expression.