relay-logic

XOR Gate: The Difference Detector That Powers Binary Addition

DigiSim Relay Team DigiSim Relay Team
9 min read

In the previous post in this series, we explored the NOR gate — and one of the most remarkable facts in all of digital logic: NOR is universal. From this single gate, replicated and rewired, you can reconstruct every other logic function that exists. The same is true of NAND. Two gates, and either one is enough to build a computer.

Universality is a powerful engineering insight. But it’s an efficiency story — a claim about what you can reduce to a single building block. It says nothing about which configurations carry their own distinct, irreducible meaning. And today’s gate has exactly that kind of personality. It isn’t universal. It can’t simulate everything on its own. What it can do is answer a specific question that none of the earlier gates could — a question that turns out to be the foundation of arithmetic.

What XOR actually asks

When you wire an OR gate, you build a circuit that answers: is at least one input high? Flip either coil from 0 to 1 and the lamp follows.

XOR — exclusive OR — asks something subtly, fascinatingly different: are the inputs different from each other?

Not “is at least one high?” but “is exactly one high — and not both?”

Work through the four cases:

  • Both inputs 0: same. Output 0.
  • Input A high, B low: different. Output 1.
  • Input A low, B high: different. Output 1.
  • Both inputs 1: same again (both high). Output 0.

That last row is the whole story. OR and XOR agree on every combination except one — the row where both inputs are 1. OR says: both are high, output is 1. XOR says: but they’re the same. Output is 0.

The symbol for XOR is ⊕, read as “A or B, but not both A and B.” You can also read it as “A is different from B.” Both descriptions map to the same four rows.

Building XOR from the gates you know

XOR doesn’t emerge from a single relay wired in a single natural way, the way AND arose from contacts in series or OR from contacts in parallel. It is a composite — assembled from the simpler operations we’ve already met — and tracing that assembly reveals something important about how complex logic is built up from simple rules.

The most direct reading:

AB=(AORB)AND NOT(AANDB)A \oplus B = (A \,\text{OR}\, B) \,\text{AND NOT}\, (A \,\text{AND}\, B)

In English: “at least one input must be high, but not both simultaneously.” Wire OR and AND from the same two inputs, invert the AND output with a NOT gate, then combine both results with a final AND gate. When A and B are different, OR fires and AND stays dark — the condition passes. When both are high, OR fires but AND fires too — and NOT(AND) blocks the final output.

There is an equivalent form that maps more directly onto relay contacts:

AB=(AAND NOTB)OR(NOTAANDB)A \oplus B = (A \,\text{AND NOT}\, B) \,\text{OR}\, (\text{NOT}\, A \,\text{AND}\, B)

Read it as: “A fires while B does not, OR B fires while A does not.” In relay hardware, this means one path through the circuit uses A’s normally-open contact in series with B’s normally-closed contact; the other path uses B’s normally-open contact in series with A’s normally-closed contact. The two paths join at the output lamp in parallel — an OR topology. Current flows through the first path only when A is 1 and B is 0; through the second path only when B is 1 and A is 0. When both inputs match — whether both off or both on — both paths go dark.

Neither expression introduces anything new. AND, OR, NOT, normally-open contacts, normally-closed contacts — every piece is from earlier posts. What’s new is the specific configuration: a circuit that asks “have these two signals diverged?” That question, it turns out, carries a profound mathematical answer inside it.

Leibniz’s 1 and 0

Gottfried Wilhelm Leibniz was many things: philosopher, mathematician, diplomat, co-inventor of calculus alongside Newton. In 1679, working in private notebooks, he laid out a complete system of binary arithmetic — a way to represent any number using only two symbols, 0 and 1. He published it formally in 1703 in a paper titled Explication de l’Arithmétique Binaire — “An Explanation of Binary Arithmetic” — where he described his fascination with the philosophical elegance of the system. All of existence, he suggested, could be encoded in the simplest possible opposition: presence and absence, one and nothing.

He wasn’t designing computers. He was writing philosophy, and he noted that the Holy Roman Emperor Kangxi of China had communicated to him that the ancient I Ching hexagrams could be read as binary numbers — Leibniz saw this as confirmation that binary truth was universal. But hidden inside his 1703 paper was the arithmetic that every processor in the world now performs billions of times per second. And the XOR gate is the heart of it.

Consider adding a single column of two binary digits. There are four cases:

ABSum bitCarry
0000
0110
1010
1101

The last row needs a moment. 1+1=21 + 1 = 2 in decimal, but in binary there is no digit “2.” The number two is written 10: zero in the current column, one carried into the next column. So the sum bit in this column is 0, and the carry is 1.

Look at the Sum bit column: 0, 1, 1, 0. Does that sequence look familiar?

That is the XOR truth table, exactly — row for row, value for value.

XOR computes the sum bit of single-digit binary addition. When Leibniz wrote 1+1=101 + 1 = 10, the digit that stays in the column — the result digit — was the output of an XOR gate. The carry bit, the one that moves to the next column, is AND: 1AND1=11 \,\text{AND}\, 1 = 1, the only row where a carry is generated.

XOR is not a logic curiosity. It is, literally, half of how computers add. Sit with that for a moment: three hundred and twenty years after Leibniz traced out his 0s and 1s and found beauty in their simplicity, a network of relays clicking in the pattern he described is performing binary addition.

The truth table in physical clothing

Two relay coils, two inputs, one lamp — framed as the concept we just derived:

ABLamp (A ⊕ B)
000
011
101
110

The lamp is a difference detector. It is off at rest — both coils quiet, no signal, no light. Fire Coil A alone: inputs differ, lamp lights. Fire Coil B alone: inputs differ, lamp lights. Fire both coils simultaneously: inputs are the same, and the lamp goes dark again — even though both coils are energized.

That final row is the moment worth sitting with. Two coils active, and the output is darker than when only one was firing. The circuit is not tracking total energy or total votes. It is tracking agreement. When A and B align, XOR reports: nothing to notice. When they diverge, XOR reports: something changed.

Compare this with OR from Post 5 one more time. OR is the generous gate: even when both inputs agree at 1, it says yes. XOR is the particular gate: when both agree, it falls silent. OR counts presence. XOR counts difference.

Try it yourself

Below is a live XOR gate — the two-path relay configuration described above — running as a genuine DC circuit simulation in your browser. It is not an animation; the relay contacts are switching and current is flowing in real time. Before you touch anything, predict: when you energize both coils together, does the lamp come on or go off?

A relay XOR gate: the lamp is on when inputs disagree, off when they agree — the difference detector in live relay hardware. Open it in the DigiSim Relay Lab.

Three experiments worth running before you move on:

  1. Compare directly with the OR gate from Post 5. Work through all four input combinations on both gates. Rows 1 through 3 are identical: both gates agree on “neither,” “A only,” and “B only.” Row 4 is where they part ways. OR lights the lamp because both inputs are present. XOR extinguishes it because both inputs are the same. One row of difference, between the most inclusive gate and the most particular one.

  2. Watch the “both on, lamp off” moment carefully. Start with one coil energized — lamp lit. Now hold it and energize the second coil. The lamp goes dark. You’ve added a signal, and the output decreased. If this surprises you, that’s the XOR gate doing exactly its job: it detected that the inputs stopped disagreeing and went silent. That reversal is not a quirk — it is the mathematical behavior of the sum bit in binary addition.

  3. Turn the sound on and count the transitions. Four rows, four state changes as you walk the table. Notice which transitions cause the lamp to change versus which don’t. The two transitions involving “same → different” or “different → same” are the ones where something changes in the output. The XOR gate clicks through the truth table in exactly the rhythm Leibniz’s arithmetic would predict.

What XOR means for computing

XOR is the gate that made addition possible in hardware. The half-adder — coming in Post 13 of this series — is nothing more than XOR and AND wired from the same two inputs: XOR for the sum bit, AND for the carry. Chain half-adders into full adders and cascade them across 8 bits, 16, 32, 64, and you arrive at the arithmetic logic unit inside every processor ever made. That entire structure of computation rests, at its foundation, on the four-row XOR truth table you just toggled.

XOR also carries weight in error detection. XOR-ing a sequence of bits together produces a simple parity bit: if any single bit flips in transmission, the parity changes. Every disk drive, network packet, and RAID array uses some descendant of this idea.

In cryptography, XOR’s symmetry makes it a fundamental cipher operation: XOR a message with a key and the result is encrypted; XOR the result with the same key again and the original message is recovered. The same gate that adds binary numbers also undoes itself — a mathematical property that Leibniz would have found as philosophically pleasing as anything in his original paper.

None of that was visible in 1703 when Leibniz traced out his 0s and 1s and marveled at their simplicity. But the arithmetic was already there, waiting for the relays to click it into existence.

What you just learned — and what’s next

Post 10 adds these pieces to your toolkit:

  • XOR is the difference detector. Output is 1 when inputs disagree; output is 0 when they agree. It is OR with the “both high” case removed — exclusive-or.
  • XOR is a composite gate: (AAND NOTB)OR(NOTAANDB)(A \,\text{AND NOT}\, B) \,\text{OR}\, (\text{NOT}\, A \,\text{AND}\, B) — assembled from AND, OR, and NOT, all of which we’ve already built.
  • XOR computes the sum bit of binary addition. Pair it with AND for the carry bit and you have a half-adder — the first piece of a working arithmetic unit.
  • Leibniz outlined binary arithmetic in 1679 and published it in 1703 — the same 0-and-1 system that XOR operates on, derived from pure mathematical philosophy three centuries before it ran in silicon.

In Post 11 of Building a Computer from Relays, we’ll invert XOR and meet its complement: the XNOR gate — the equality detector. Where XOR fires when inputs disagree, XNOR fires when they agree. It’s the XOR truth table turned upside down, one NOT gate away. And like its sibling, it will find uses in arithmetic and error-checking that go far beyond the obvious.

The clicks are adding up. Every circuit in this series is live and waiting in the DigiSim Relay Lab.