Digital Logic
What is an XOR gate?
An XOR gate, short for exclusive OR, outputs 1 when its two inputs are different and 0 when they match. It's "one or the other, but not both". It's written A ⊕ B, and it's the gate that adds bits, checks parity, runs two-way light switches and scrambles data.
The truth table
| A | B | A OR B | A XOR B |
|---|---|---|---|
| 0 | 0 | 0 | 0 |
| 0 | 1 | 1 | 1 |
| 1 | 0 | 1 | 1 |
| 1 | 1 | 1 | 0 |
XOR and OR agree on three rows. The last row, both inputs on, is the whole difference, and it's the row that makes XOR useful. Written with AND, OR and NOT, it's one AND term for each row that gives a 1:
The staircase light
A light with a switch at the bottom of the stairs and another at the top follows XOR's rule. Flip either switch and the light changes, whatever the other one is doing. With OR instead, once both switches are up the light can't be turned off from either end.
Live experiment
Light the stairs
A switch at the bottom of the stairs and one at the top both feed the gate that drives Pip, the landing light. Wired with XOR, either switch flips the light: switch it on downstairs, off upstairs. Then wire it with OR and try the same trip.
“Same or different” is lesson 5 of 6 in Gate Grove, Digital Logic World 1. Free with an account. Lessons unlock in order, so you’ll do the 4 before it first.
Adding two bits
Add two bits and the sum column reads 0, 1, 1, 0: XOR's table. The carry reads 0, 0, 0, 1: AND's. So one XOR and one AND fed by the same two inputs make a half adder, the first step of every adder in a processor:
More tricks with XOR
- Parity: chain XORs across a byte and the output is 1 when an odd number of bits are 1. Add that as a parity bit and a single flipped bit in transit shows up.
- Inverting on demand: A ⊕ 0 = A and A ⊕ 1 = Ā. One input decides whether the other passes straight through or comes out flipped, which is how an adder becomes a subtractor.
- Spotting a difference: XOR two numbers bit by bit and every 1 in the answer marks a bit that differs.
- Simple encryption: XOR a message with a secret key to scramble it, and XOR it with the same key again to get it back, since (M ⊕ K) ⊕ K = M.
Free lesson
Now build it yourself
Each lesson comes in three short parts: see the idea, try it on a live board, then build it yourself, with quick checks as you go.
“Same or different” is lesson 5 of 6 in Gate Grove, Digital Logic World 1. Free with an account. Lessons unlock in order, so you’ll do the 4 before it first.
Or start from the very first lesson, no account neededWork it out
Practice questions
Three quick questions from the lessons. Pick an answer to check it.
Show the answer
Answer: 0: they're the same. XOR means one or the other but not both. Both on is a match, and matches give 0. That's the one line where XOR and OR disagree.
- Why not “It depends”? XOR's output depends only on its inputs, and matching inputs always give 0.
- Why not “1: at least one is on”? At least one on is OR's rule. XOR gives 1 only when exactly one input is 1, so two 1s give 0.
- Why not “2”? A gate's output is a single bit. 1 + 1 is 10 in binary, and XOR gives only the sum bit, 0.
Show the answer
Answer: XOR. The light is on when the two switches disagree, and flipping either one makes them agree or disagree. That's XOR.
- Why not “OR”? With OR, once both switches are up the light stays on, so flipping one switch doesn't always change it.
- Why not “NOT”? NOT has a single input, so it can't respond to two switches.
- Why not “AND”? With AND, flipping one switch while the other is down changes nothing: the light stays off.
Show the answer
Answer: Stays on: OR still says 1 with both up. With both switches up, OR gives 1, so the top switch can't turn it off. XOR gives 0 there, which is why stairs use XOR's rule.
- Why not “Flickers”? A gate's output settles straight away, and with OR both switches up is a steady 1.
- Why not “Gets brighter”? A logic output is just 1 or 0. Two 1s into an OR give the same 1 as one.
- Why not “Goes off”? Going off is what XOR would do. OR with both switches up still gives 1.
“Same or different” is lesson 5 of 6 in Gate Grove, Digital Logic World 1. Free with an account. Lessons unlock in order, so you’ll do the 4 before it first.
Questions people ask
What is the difference between OR and XOR?
They agree on three rows of the truth table. They differ only when both inputs are 1: OR gives 1, because at least one is on, and XOR gives 0, because the inputs match. XOR means one or the other, but not both.
What does an XOR gate with three inputs do?
It outputs 1 when an odd number of its inputs are 1, and 0 when an even number are. Chained XORs work the same way for any number of bits, which is exactly what a parity checker needs.
What is XNOR?
XOR with its output inverted: 1 when the inputs match, 0 when they differ. It's an equality detector, and a row of them ANDed together tells you whether two binary numbers are equal.
Related topics
- Logic gates explainedAND, OR, NOT, XOR and their bubbled twins: each one's rule is a truth table.
- Half adder vs full adderA half adder adds two bits; a full adder adds the carry too, so they chain.
- What is a parity bit?One extra bit that makes the count of 1s even, so a single flip shows up.
- How to add binary numbersColumn by column, right to left, carrying when two 1s meet: the same sum every adder chip does.