Computing Library › Classical Logic Gates
Classical Logic Gates

XOR Gate

The XOR gate outputs 1 when its inputs differ, implementing exclusive-OR and modulo-2 addition of bits.

What it does

XOR, exclusive-OR, outputs 1 when its inputs are different and 0 when they are the same. For two inputs it answers the question, does exactly one input equal 1.

Truth table

Kronos motion — when
ABA XOR B
000
011
101
110

Algebraic form

XOR is written with a plus in a circle. It equals A AND NOT B, OR NOT A AND B. It is commutative and associative, and it satisfies A XOR 0 = A and A XOR A = 0. XOR with all-ones inverts a value, so XOR is a controllable inverter.

Modulo-2 addition

XOR is addition without carry in base two. This makes it the sum bit of a half adder and the workhorse of parity, checksums, and linear-feedback shift registers.

In code

python
out = a ^ b        # bitwise XOR
parity = 0
for bit in bits:  # parity is XOR of all bits
    parity ^= bit

Where it appears

XOR needs several simpler gates to build, but its distinctive difference behavior earns it a dedicated symbol.