3.1.6 · D2Boolean Algebra & Logic Gates

Visual walkthrough — XOR, NAND, NOR, XNOR gates

2,232 words10 min readBack to topic

Before we start, we agree on the alphabet.

Our star gate:


Step 1 — Draw the target so we know what "done" looks like

WHAT. Before wiring anything, we pin down exactly what XOR must do: output when the two inputs disagree, when they agree.

WHY. You can't build a machine without its spec. The XOR truth table is the spec — four rows, four answers. Every later step is checked against these four answers.

PICTURE. The four input pairs live at the corners of a little square. Colour a corner pale yellow if XOR there is , leave it dark if . Notice the two lit corners are the diagonal — the "different" corners.

Figure — XOR, NAND, NOR, XNOR gates
0 0 0
0 1 1
1 0 1
1 1 0

Step 2 — Make NOT from one NAND (our first tool)

WHAT. Feed the same bit into both inputs of a NAND. Out comes its opposite.

WHY. XOR's formula contains and — flipped inputs. We have no NOT gate in our box, only NAND. So the very first thing we need is a way to flip a bit using NAND alone.

Here is the algebra, one symbol at a time:

  • — ANDing a bit with itself gives the bit back (, ); this is idempotence,
  • — the NAND's built-in flip then turns it into .

PICTURE. One NAND gate, both input wires tied to the same source. Trace : inside, , bubble flips to . Trace : , bubble flips to . It's a NOT.

Figure — XOR, NAND, NOR, XNOR gates

Step 3 — The clever middle wire: compute once

WHAT. Compute a single helper bit and reuse it in two places.

WHY. The naive XOR needs and — that looks like it wants separate NOTs and ANDs everywhere. The trick that gets XOR down to four NANDs is spotting that one shared value does double duty. Let's tabulate so we can see what it gives us.

  • — AND the raw inputs,
  • overbar — flip it, so only at the corner.
0 0 0 1
0 1 0 1
1 0 0 1
1 1 1 0

PICTURE. One NAND fed by and . Its output wire (drawn in chalk blue) splits and heads to two places — the two NANDs of the next step.

Figure — XOR, NAND, NOR, XNOR gates

Step 4 — Build the two side branches and

WHAT. NAND each input with the shared :

WHY. These two combinations are engineered so that becomes exactly at one "different" corner and at the other. We compute them as tables and watch the diagonal appear.

  • — AND input with the helper ,
  • overbar — flip; only when and together.
0 0 1 0 1 0 1
0 1 1 0 1 1 0
1 0 1 1 0 0 1
1 1 0 0 1 0 1

PICTURE. Two NAND gates side by side: the top takes and giving ; the bottom takes and giving . See how dips to on the row and dips to on the row — each grabs one diagonal corner.

Figure — XOR, NAND, NOR, XNOR gates

Step 5 — The final NAND stitches the diagonal together

WHAT. NAND the two branches: output .

WHY. is low at one different-corner, is low at the other. A NAND of gives whenever either of them is — precisely the two "different" corners. That is XOR.

  • — AND the two branches; this is only when both branches are (the "same" corners),
  • overbar — flip, so exactly when at least one branch was (the "different" corners).
want
0 0 1 1 1 0 0 ✅
0 1 1 0 0 1 1 ✅
1 0 0 1 0 1 1 ✅
1 1 1 1 1 0 0 ✅

PICTURE. The last NAND merges and into . The output column lights up on exactly the diagonal from Step 1 — spec met.

Figure — XOR, NAND, NOR, XNOR gates

Step 6 — Walk all four corners by hand (no case skipped)

WHAT. We now trace every input pair through all four gates, so no reader ever meets a corner we didn't show.

WHY. A truth table proves what; tracing proves how the electrons flow. Cover — the complete input space of two bits.

  • : ; ; ; . Same → . ✅
  • : ; ; ; . Different → . ✅
  • : ; ; ; . Different → . ✅
  • : ; ; ; . Same → . ✅

PICTURE. Four mini copies of the full circuit, each with its bits written on every wire — a filled-in worksheet of all cases.

Figure — XOR, NAND, NOR, XNOR gates

Step 7 — The degenerate check: what if ?

WHAT. Feed identical bits () and confirm the output collapses to .

WHY. The whole point of XOR is "difference detector". So when there is no difference, it must read . This is the edge case that separates XOR from plain OR (which the parent warned about).

  • : from Step 6, . Agree → . ✅
  • : from Step 6, . Agree → . ✅

Compare with OR, which would give at . Our NAND-built XOR correctly gives there — this single corner is the entire difference between OR and XOR.

PICTURE. The two "equal" corners highlighted on the square, both dark (), with an arrow pointing at labelled "where OR but XOR ".

Figure — XOR, NAND, NOR, XNOR gates

The one-picture summary

Everything on one board: raw inputs → NAND to → two NANDs to → final NAND to , with the four-corner truth table riding alongside.

Figure — XOR, NAND, NOR, XNOR gates
Recall Feynman retelling — explain it to a friend

Imagine you only own one kind of LEGO brick: the NAND brick, which lights up unless both its studs are pressed. First trick: press both studs with the same finger and the brick becomes a "flipper" (NOT) — that's Step 2. Now the plan: take your two switches and , and make one shared clue brick that goes dark only when both switches are on. Hand that clue to two more bricks — one paired with , one paired with . The -brick goes dark only in the corner where " is on but they differ"; the -brick goes dark only in the opposite different-corner. The last brick watches those two: it lights up whenever either one went dark — i.e. whenever the switches disagree. Press both switches the same way and nothing goes dark, so the last brick stays off. Four identical bricks, and you've built "are these two different?" — the exact gate inside every adder and parity checker. That's why chip factories only need to master one brick.

Recall

equals?
— tying both inputs together turns a NAND into a NOT.
The shared helper in the 4-NAND XOR equals?
(i.e. ), reused by both branches.
At which input pair do OR and XOR disagree?
— OR gives , XOR gives .
How many NAND gates build an XOR here?
Four.
Full NAND-only expression for XOR?
.
Why trace all four corners instead of one?
A faulty circuit can match on 3 rows and fail the 4th; only all cases prove correctness.

Connections

  • Parent: XOR, NAND, NOR, XNOR gates — this page derives its 4-NAND XOR in pictures
  • Universal Gates — why one NAND type suffices for everything
  • AND, OR, NOT gates — the primitives we re-earned from NAND
  • Boolean Algebra Laws — idempotence () and De Morgan behind the tricks
  • Sum of Products (SOP) — the target we matched
  • Half Adder and Full Adder — this XOR is the sum bit
  • Parity Bits & Error Detection — chained XOR = parity
  • Karnaugh Maps — simplifying such gate expressions

Concept Map

into

into

into

shared

into

shared

combine

combine

input A

input B

M = A NAND B

P = A NAND M

Q = B NAND M

Y = P NAND Q = A XOR B