
Code: The Hidden Language of Computer Hardware and Software
How computers really work
Description
Picture two kids in neighboring houses, bedroom windows facing each other across a dark yard. They want to talk after their parents have said goodnight, and they only have flashlights. So they invent a system: one blink for A, two for B, and so on down the alphabet. It's slow and tedious, and pretty soon they're looking for shortcuts. That scene is where Charles Petzold begins Code, first published in 1999 and expanded for a second edition in 2022. It sounds like a story for children. It is, in fact, the whole book in miniature.
Because everything a computer does grows out of that flashlight. A signal that can be on or off. A code that turns blinks into letters. A wire that carries the signal further than a bedroom window. Petzold's wager is that if we start from something as humble as two kids and a pair of flashlights, and never skip a step, we can build our way up to a working computer without ever being asked to take anything on faith. No hand-waving, no "and then a miracle happens." Just one small, comprehensible move after another.
That patience is what makes Code unusual. Most explanations of how computers work either drown us in jargon or wave vaguely at "ones and zeros" and hope we stop asking. Petzold does neither. He treats the reader as someone perfectly capable of understanding the real thing, given the right order of ideas. The promise is quietly radical: by the last page, we can follow the logic all the way from a blinking light to a machine that adds, remembers, and obeys.
The question we’re asking : How does a computer actually work, starting from nothing more than a signal that's either on or off?What we’ll see : How Petzold builds a real machine out of the simplest possible parts, one honest step at a time.
Table of contents
01Chapter 1 — A flashlight blinking across the yard
The flashlight is not a gimmick. It's the cleanest way to introduce the single idea the entire book rests on: a signal with two states. On or off. Blink or dark. Petzold points out that Morse code, invented in the 1840s, already solved most of the problem our two kids are wrestling with. Samuel Morse's dots and dashes are a way of representing every letter of the alphabet using nothing but short and long — two symbols, arranged in patterns. The kids with flashlights are reinventing telegraphy without knowing it.
What Morse code teaches, and what Petzold wants us to feel in our bones, is that any information can be carried by a system with only two possibilities, as long as we agree in advance what the patterns mean. A short blink and a long blink are enough to spell anything. The catch is efficiency: common letters like E should get short codes, rare ones like Q can afford long ones. Morse understood this, which is why E is a single dot and Q is dash-dash-dot-dash. Encoding, it turns out, is partly a question of guessing what shows up most often.
02Chapter 2 — From telegraphs to the invention of the switch that thinks
To turn a code into a machine, we need a way to send signals down a wire and, crucially, a way to control them. Petzold walks through the basics of electricity — a battery, a wire, a bulb, a switch — because these are the physical bricks. Close the switch and current flows; the bulb lights. Open it and the bulb goes dark. That's the flashlight again, now made of copper and voltage. Wire two switches in a row and the bulb only lights if both are closed. Wire them side by side and it lights if either is. Without naming it yet, we've just built logic out of hardware.
The historical hinge is the telegraph, which by the mid-nineteenth century sent Morse code across continents. But a telegraph signal fades over long distances, so engineers needed something to boost it. The solution was the relay: a switch operated not by a finger but by electricity itself. A weak incoming current energizes an electromagnet, which pulls a metal arm shut, closing a second circuit and sending a fresh strong signal onward. The relay lets one electrical signal control another. Petzold lingers here because this is the whole game.
03Chapter 3 — Building an adder out of nothing but relays
Here Petzold does the thing most books refuse to do: he actually builds an adding machine, on paper, out of the logic gates from the previous chapter. The first requirement is a way to write numbers using only two states, because that's all our switches understand. That's binary — counting with just 0 and 1, where each position is a power of two instead of ten. It looks strange at first, but Petzold shows it follows the same rules we learned as kids, only with a smaller base. In binary, 1 plus 1 is 10 — carry the one, exactly like adding 5 and 5 in the everyday system.
The genius of the chapter is showing how ordinary binary addition breaks down into logic. When you add two binary digits, two things can happen: you get a result digit, and you might get a carry into the next column. Petzold notices that the result digit is on precisely when one input OR the other is on, but not both — that's a gate. And the carry is on only when both inputs are on — that's another gate. Put those two gates together and you have a circuit that adds two single bits. It has a name, the half adder, but the name matters less than the fact that it is built entirely from relays clicking on and off.
04Chapter 4 — The machine that remembers, and follows orders
An adder that instantly forgets its answer isn't much use. So Petzold shows how the same relays that compute can be wired to remember. Take two gates and feed each one's output back into the other's input, and you get a circuit that latches: flip it on and it stays on, flip it off and it stays off, holding its state until told otherwise. This feedback loop, the flip-flop, is memory made from the exact same parts as everything else. Stack enough of them and you have a place to store numbers — the ancestor of every gigabyte in your phone.
With arithmetic and memory in hand, the last ingredient is control: a way to tell the machine which operation to perform and on which numbers, one step at a time. Petzold introduces the idea of instructions encoded as numbers, stored in memory alongside the data. The machine reads an instruction, does what it says — add these, store that, fetch the next — and moves on. This is the leap from a calculator to a computer. The steps themselves become data the machine can read, which means a machine's behavior can be changed by changing the numbers in its memory rather than rewiring it.
05Conclusion
By the end, the two kids and their flashlights turn out to have been holding the whole computer in their hands. A signal that's on or off, a code agreed in advance, a way to carry it further than a bedroom window. Petzold's achievement is to walk the distance from that yard to a working processor without ever asking the reader to trust him. Every wire is drawn, every gate is traced, every carry is followed. The machine that seemed to belong to a priesthood of engineers turns out to be assembled from parts a curious person can genuinely hold in mind.













