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
| A | B | A XOR B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |
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
out = a ^ b # bitwise XOR
parity = 0
for bit in bits: # parity is XOR of all bits
parity ^= bit
Where it appears
- Parity generation and checking for error detection.
- Comparing two values: any difference makes the XOR output 1.
- Adders, where XOR forms the sum and AND forms the carry.
- Cryptographic mixing and toggle logic.
XOR needs several simpler gates to build, but its distinctive difference behavior earns it a dedicated symbol.