XOR: The “Exactly One” Logic Gate
1. What Does “Exactly One” Mean? The XOR Truth Table
Imagine you have two light switches and a special bulb that lights up only when exactly one switch is ON. That is XOR in a nutshell. In Boolean logic, inputs and outputs can only be TRUE (1) or FALSE (0). For two inputs A and B, the XOR output $A \oplus B$ follows a simple rule: it is 1 if A and B are different, and 0 if they are the same. The table below shows all four possibilities.
| Input A | Input B | XOR Output (A ⊕ B) |
|---|---|---|
| 0 | 0 | 0 (same inputs → false) |
| 0 | 1 | 1 (different inputs → true) |
| 1 | 0 | 1 (different inputs → true) |
| 1 | 1 | 0 (same inputs → false) |
Notice how XOR behaves like “not equal”. In fact, for two bits, $A \oplus B$ is exactly the same as $A \neq B$. This is why XOR is often called a difference detector.
2. The Boolean Algebra Behind XOR
Mathematically, XOR can be built from the basic AND, OR, and NOT operations. The standard formula is:
Here the overbar means NOT, the dot $\cdot$ means AND, and the $+$ means OR. The expression says: “A is true and B is false, OR A is false and B is true.” Exactly the condition for exclusive OR. You can also think of XOR as a parity function: it returns 1 if an odd number of inputs are 1. For two inputs, “odd” means exactly one.
3. Extending XOR: Three or More Inputs
What happens when you have three inputs A, B, and C? XOR remains associative, so $A \oplus B \oplus C$ means you apply XOR to the first two, then XOR that result with the third. The rule becomes: output is TRUE if the number of true inputs is odd. For three inputs, that means true when exactly one input is true OR when all three are true (since three is odd). Let’s see it in action.
| A | B | C | A ⊕ B ⊕ C |
|---|---|---|---|
| 0 | 0 | 0 | 0 |
| 0 | 0 | 1 | 1 |
| 0 | 1 | 0 | 1 |
| 0 | 1 | 1 | 0 |
| 1 | 0 | 0 | 1 |
| 1 | 0 | 1 | 0 |
| 1 | 1 | 0 | 0 |
| 1 | 1 | 1 | 1 |
4. Where XOR is Used: Parity and Controlled Inversion
XOR is a workhorse in computer engineering. One classic use is parity generation for error detection. When sending data, an extra bit (the parity bit) is added to make the total number of 1s either even (even parity) or odd (odd parity). XOR computes this efficiently. For even parity, the parity bit is simply the XOR of all the data bits.
If the data bits are 1,0,1,1 (three 1s), then $P = 1 \oplus 0 \oplus 1 \oplus 1 = 1$ (odd). To make the total even, we actually want the opposite: for even parity we need the XOR of all five bits (including P) to be 0. That’s why parity is often implemented with XOR trees.
Another vital use is a controlled inverter. If you take an input A and XOR it with a control C, then:
- If $C=0$, output = $A \oplus 0 = A$ (no change).
- If $C=1$, output = $A \oplus 1 = \overline{A}$ (flipped).
This property is used in cryptography (stream ciphers) and in arithmetic circuits. In fact, a half adder—the simplest circuit for adding two bits—uses XOR for the sum bit: $Sum = A \oplus B$ and AND for the carry.
5. Important Questions About XOR
A: For two Boolean inputs, yes. The truth table for $A \neq B$ is identical to $A \oplus B$. However, for more than two inputs, “not equal” is not a standard operation, while XOR is well-defined as odd parity.
A: Use the formula $A \oplus B = (A \cdot \overline{B}) + (\overline{A} \cdot B)$. You need two AND gates, two NOT gates, and one OR gate. Alternatively, you can use NAND gates only.
A: Zero. Since both inputs are the same (both 0 or both 1), the XOR rule says output is 0. This property makes XOR useful for clearing registers: $X \oplus X = 0$.
Footnote
[1] XOR: Exclusive OR. A logical operation that outputs true only when the inputs are different (for two inputs) or when an odd number of inputs are true (for any number).
[2] Boolean algebra: A branch of algebra dealing with true/false values and logical operations (AND, OR, NOT).
[3] Parity: A technique that counts the number of 1-bits in a binary sequence. Even parity means the total number of 1s is even; odd parity means it is odd.
[4] Half adder: A digital circuit that adds two bits, producing a sum (XOR) and a carry (AND).
