Exclusive OR (XOR)

We already know the OR gate — it outputs a 1 if at least one of its inputs is on. But mathematics and programming often need stricter logic: a choice of "either this, or that, but not both". For that there is the Exclusive OR gate — XOR.

The XOR level solution: exclusive OR from basic gates
The XOR gate: authored level solution

Imagine your mom said: "You can have either ice cream or a chocolate bar." If you eat just one thing — you're great (result 1). If you refuse everything — that's strange (result 0). And if you scarf down both the ice cream and the chocolate — the condition is broken (result 0).

That's exactly how XOR works: it outputs 1 only when the signals on its inputs are different — one is 1 and the other is 0. If the inputs are the same (two ones or two zeros), XOR outputs 0.

XOR truth table

ABA XOR BInterpretation
000Inputs are the same
011Inputs differ
101Inputs differ
110Inputs are the same

XOR is a difference detector: it answers the question "are these two bits different?" Yes — one, no — zero. Neither AND nor OR can do this: OR fires even when both inputs are 1, and AND can't tell 01 from 10.

The XOR formula

XOR can be expressed through gates you already know:

XOR(A, B) = (A OR B) AND NOT (A AND B)

Read the formula in words: "either A, or B, but not both at once." First, OR checks that there's a one on at least one input; then AND + NOT check that there aren't two ones. Both conditions must hold — that's why they're joined with an AND.

In the game, in level 1.5, you'll build XOR exactly this way — from OR, NAND, and AND gates.

XOR as a controlled inverter

XOR has a remarkable property: it can "switch" a signal. Let A be the data and B the control signal. Let's work through the numbers for A = 1:

• B = 0: XOR(1, 0) = 1 — the data passed through unchanged.
• B = 1: XOR(1, 1) = 0 — the data got flipped.

That's a "controlled inverter": while the control is 0, the signal passes as-is; as soon as the control becomes 1, every bit flips. This is exactly how the simplest hardware encryptors work: apply a key with XOR — the data is encrypted, apply the same key again — it's decrypted back, because XOR(XOR(A, K), K) = A.

XOR and addition: the sum bit

XOR's most important use is binary arithmetic. Let's add two bits: 0+0=0, 0+1=1, 1+0=1, but 1+1 in binary is 10: we write zero and "carry" a one. Look at what XOR outputs: 0, 1, 1, 0 — that's exactly the sum bit! And the carry (the one "in our head") is the result of AND: only for 1+1.

That's why the half adder in level 1.6 is built like this: Sum = A XOR B, Carry = A AND B. All processor arithmetic starts with this gate.

Common mistakes

• Forgetting that XOR outputs 0 on inputs 11 — that's its difference from OR.

• Getting the controlled inverter wrong: with control 1 the data flips, not stays the same.

Summary

1. XOR outputs 1 only when the inputs differ: 01 → 1, 10 → 1, while 00 and 11 → 0.

2. The XOR formula: (A OR B) AND NOT(A AND B) — "either one or the other, but not both".

3. XOR is a controlled inverter: control 0 passes data through, control 1 flips it.

4. Applying a key with XOR twice restores the original data — that's how simple encryption works.

5. The sum bit of a half adder is A XOR B, and the carry is A AND B.

In level 1.5 you will build the XOR gate from the ready-made OR, NAND, and AND gates — and see how "strict choice" is born from simple rules. If you want to understand why any gate can be built from NAND, check out De Morgan's laws, and for binary addition in more detail, read Binary Math for Beginners.

Try it in the simulator →