Chapter 2
What can gates do?

  1. The browser
  2. The network
  3. Software and languages
  4. The CPU
  5. Memory
  6. Arithmetic
  7. Logic gates
  8. The switch
Figure 2.1: Where we are in the climb from transistor to browser: the logic-gates rung. This chapter turns NAND into the full set of gates, the toolkit every circuit ahead is built from.

Chapter 1 closed with one gate and a promissory note: NAND alone is enough. This chapter cashes it. Starting from nothing but NAND calls, the program builds every gate the rest of the book will use: OR, NOR, XOR, and XNOR. By the end you will have met the short algebra of 0 and 1 that organises the whole family and seen why a four-gate network can detect whether two bits disagree, which is the seed of every adder ever built.

2.1 NAND is universal, made concrete

Chapter 1 showed two derivations: tie both inputs of a NAND together and you get NOT; feed the output of a NAND through that NOT and you recover AND. Those two gates, together with NAND, span the full set of Boolean functions. In code the derivation is already done: the switch chapter’s code, collected in switch.py, exports not_ and and_, both written in terms of nand, with no built-in boolean operator in sight. The rest of the gates are more of the same. Each is a short composition of nand calls; each truth table drops out of the algebra without being typed in by hand.

2.2 OR, NOR, and the De Morgan bridge

OR is the first gate that needs a conceptual lever: De Morgan’s first law. The law says that NOT(NOT a AND NOT b) equals (a OR b). Inverting both inputs of a NAND turns AND into OR. In circuitikz terms: put an inversion bubble on each input of a NAND and you have drawn an OR gate. Figure 2.2 puts the two symbols side by side so the equivalence is visible as geometry.

de-morgande-morgan
Figure 2.2: De Morgan: a NAND and an OR with inverted inputs are the same gate. The bubble on each input of the right-hand symbol stands for the NOT that De Morgan’s law inserts. Both symbols produce identical outputs for every input pair.

The code is as short as the law:

Example 2-1. OR from NAND, via De Morgan

def or_(a: int, b: int) -> int:
    """OR via De Morgan: nand of the two inverted inputs."""
    return nand(not_(a), not_(b))1

NOR adds only an outer inversion. nor_(a,b) calls not_(or_(a,b)) and nothing else; the three-gate chain still contains nothing but nand.

2.3 XOR, the odd one out

XOR outputs 1 when its inputs differ. The algebraic identity is ab = (ab) ∧ (ab), but the standard NAND implementation avoids OR entirely and uses four NANDs in three levels: a shared NAND, two NANDs combining it with each input, then an output NAND. Figure 2.3 shows both OR and XOR side by side, making the structural contrast plain.

gates-from-nandgates-from-nand
Figure 2.3: OR and XOR, each built from NAND gates. Left: OR as two inverters feeding one NAND, the De Morgan construction. Right: XOR as four NANDs in three levels: a shared NAND, two NANDs combining it with each input, then an output NAND.

The interactive version needs the in-browser runtime.

The key trick in XOR is computing nand(a, b) once and reusing the result in both second-level gates. Computing it twice would be redundant; the single shared value is the structural reason the network works.

Example 2-2. XOR from four NANDs

def xor_(a: int, b: int) -> int:
    """XOR from four NANDs across three levels."""
    shared = nand(a, b)1
    return nand(nand(a, shared), nand(b, shared))2

Note that XOR foreshadows the adder in Chapter 3: this gate, beside one other you already have, will turn out to be an arithmetic circuit with nothing added. The four-NAND network here is part of the skeleton of every binary adder.

2.4 Boolean algebra: the algebra of 0 and 1

The gates make sense in isolation, but they make even more sense as a system. Boolean algebra is the algebra whose variables take only the values 0 and 1 and whose three operations correspond directly to the gates above:

A handful of identities follow directly from those definitions and are worth memorising because they recur constantly:

a + 0 = a a · 1 = a
a + 1 = 1 a · 0 = 0
a + a = a a · a = a
a + a = 1 a · a = 0

De Morgan’s two laws tie AND and OR together through complementation:

a · b = a + b a + b = a · b

The first law is what made OR possible in one NAND call: a + b is a sum (OR), which equals NOT(AND), which is NAND. The second law would let you build NOR from a single AND-with-inverted-inputs in the same way.

The four gates built here, together with NOT, AND, and NAND from Chapter 1, are the complete toolkit for the arithmetic and memory circuits ahead.

Exercises

PreviousNext
✎ Suggest an edit