XOR in Arithmetic and Logic
The exclusive-OR gate is the workhorse of digital arithmetic, comparison, parity, and controllable inversion.
What XOR does
XOR outputs 1 when its inputs differ. For two bits, A XOR B is 1 for inputs 01 and 10, and 0 for 00 and 11. This simple difference detector appears throughout digital design.
Truth table
| A | B | A XOR B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |
In addition
The sum bit of both the half adder and full adder is an XOR of the operand bits. XOR is modulo-2 addition, which is exactly what a single sum column computes before the carry.
Controllable inverter
A XOR C inverts A when the control C is 1 and passes A unchanged when C is 0. This trick turns an adder into a subtractor and appears wherever conditional negation is needed.
Parity and comparison
XORing all bits of a word gives its parity, used for error detection. XNOR, the complement, tests equality, which is why comparators use it. XOR of a value with itself is always 0, a fact used to clear registers.
In code
parity = 0
for bit in bits:
parity ^= bit