Computing Library › Digital Logic & Circuits
Digital Logic & Circuits

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

Kronos motion — thermal gate
ABA XOR B
000
011
101
110

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

python
parity = 0
for bit in bits:
    parity ^= bit