Logic From Switches
Tap the board to zoom inClick the board to zoom in
Lessons that use this board
LOGIC FROM SWITCHES
How Computers Work · 03 — transistor → gates → an adder → memory: how you build ARITHMETIC out of switches
a computer is billions of one component: a switch. wire switches together and you get LOGIC; wire logic together and you get ADDITION and MEMORY. that's the whole machine.
1 · THE TRANSISTOR = a switch you control with electricity
a MOSFET has 3 legs: source, drain, and a GATE. put voltage on the gate → current flows source→drain
(switch CLOSED = 1). no voltage → no flow (switch OPEN = 0). that is the entire trick.
source
drain
GATE (control voltage)
voltage on the gate CLOSES the switch → current flows → we read a 1.
no moving parts, switches billions of times a second. a modern chip (Apple M-series) packs ~20 BILLION
of them; Moore's law was the decades-long doubling of how many fit on a chip. FLASH memory is named
after the NAND gate. the transistor is the atom — everything above is just wiring.
2 · LOGIC GATES (a few transistors each)
wire transistors so the output depends on the inputs by a rule. the truth tables ARE the definitions:
NOT (invert)
A|Y 0|1 1|0
AND
AB|Y 00|0 01|0 10|0 11|1
1 only if BOTH
OR
AB|Y 00|0 01|1 10|1 11|1
1 if EITHER
XOR
AB|Y 00|0 01|1 10|1 11|0
1 if they DIFFER
NAND
AB|Y 00|1 01|1 10|1 11|0
= NOT-AND
NAND is UNIVERSAL: wire enough NANDs and you can build EVERY other gate — and thus every circuit,
including the whole CPU. (NOR is universal too.) so really the machine needs just ONE kind of gate.
gates so far are COMBINATIONAL: output = a pure function of the inputs, no memory (yet).
3 · BUILD AN ADDER — arithmetic falls out of two gates
HALF ADDER: add two bits A + B. the SUM is A XOR B; the CARRY is A AND B. that's it.
A
B
XOR
AND
SUM
CARRY
0+0=0 c0 0+1=1 c0
1+0=1 c0 1+1=0 c1 <- carry!
FULL ADDER: add A + B + carry-IN (from the column to the right) → SUM + carry-OUT. = two half-adders + an OR.
RIPPLE-CARRY ADDER: chain 8 (or 32, or 64) full adders, feeding each carry into the next →
full add
full add
full add
full add
full add
bit0
bit1
bit2
...
bit n
carries ripple left →
this IS the adder inside the CPU's ALU. and because of two's complement (lesson 02), this SAME adder
also does SUBTRACTION — no extra circuit needed. subtract = add the negative.
4 · MEMORY FROM FEEDBACK & the abstraction ladder
to REMEMBER a bit, feed a gate's output back into its own input. the loop HOLDS a value:
NAND
NAND
cross-coupled → the
circuit stays in its last
state = 1 BIT of memory.
add a clock signal → a D FLIP-FLOP: stores 1 bit, updated on each tick. THIS is what
registers and SRAM (your CPU cache) are made of — combinational logic can't remember; feedback can.
THE ABSTRACTION LADDER (why you can forget the layer below):
transistor (a switch)
gate (AND / OR / NOT / XOR)
adder + flip-flop (math + memory)
register / ALU
CPU (lesson 04)
each layer HIDES
the one below —
the core idea of
all computing.
JARGON, DECODED
transistor (MOSFET) = a voltage-controlled switch · logic gate = a circuit computing a boolean rule · truth table = the gate's full input→output map · combinational = output depends only on current inputs
AND/OR/NOT/XOR = both/either/invert/differ · NAND (NOT-AND) = UNIVERSAL — builds every other gate · half adder = XOR(sum)+AND(carry) · full adder = adds a carry-in · ripple-carry = chained full adders
ALU = arithmetic logic unit (the adder + more) · flip-flop / latch = 1 bit of memory via feedback · SRAM = fast memory made of flip-flops (cache) · clock = the tick that advances flip-flops
abstraction ladder = each layer built from the one below and hiding it: transistor → gate → adder/register → CPU. Moore's law = transistors-per-chip roughly doubling every ~2 years.
Saved in this browser only.