All episodes · From One Switch to a CPU · Episode 4

5 × 3 With AND Gates: How Computers Multiply and Divide

Pressing play loads the video from YouTube.

No times table, nothing memorized: a grid of AND gates and the adders we already built work out five times three.
We redo long multiplication in binary (shift and add), wire up a 4-bit multiplier, divide thirteen by three the binary way (shift, compare, subtract), and fix a problem hiding in every adder: the carry that ripples from column to column, and the carry-lookahead trick real processors use to add 64 bits in a fraction of a nanosecond.

Every animation in this video is drawn by code, and the narration is an AI voice.

Transcript

Five times three. No times table, nothing memorized. Just a grid of AND gates and the adders we already built... and out comes fifteen. Today: how a computer multiplies, how it divides, and why even adding needs a shortcut.

Remember long multiplication? Twenty-three times twelve. Multiply by the two, then by the one, shifted one place left. Then add the rows. In binary, it gets much easier, because every digit is zero or one. Multiplying by one copies the number. Multiplying by zero gives all zeros. That's the whole times table. Five times three. Three is zero, zero, one, one. For each one in three, write down five, shifted to that column. Five, plus five shifted once, which is ten. Add them up: fifteen. Shift and add. That's all multiplication is in binary.

To build it, notice that multiplying by one bit is just an AND gate: a bit of five, AND a bit of three. Four bits times four bits needs sixteen AND gates, in a grid. Each row is one shifted copy of five, or all zeros. Then rows of adders add the copies together, column by column, carries and all. Four bits times four bits can need eight bits. Fifteen times fifteen is two hundred twenty-five. And shifting costs nothing. It's just wiring: every bit moves one column over. That's why multiplying by two is the cheapest trick a computer has: shift every bit one place left.

Division is long division, done in binary: shift, compare, subtract. Thirteen divided by three. Start from the left. Bring down a one. Is three bigger? Yes. Write a zero. Bring down the next bit. Now we have one, one: that's three. It fits. Write a one, and subtract. Bring down a zero: too small, write zero. Bring down a one: still too small, write zero. The answer is zero, one, zero, zero: four, with one left over. Thirteen is three times four, plus one. Each step is one subtraction and one look at the sign: the same adder and flags from the last two episodes. And because each step needs the answer from the step before, division is the slowest of the four basic operations.

Now back to adding, because there's a problem we've been ignoring. Each column waits for the carry from the column on its right. Four columns, four waits. With sixty-four columns, the last column waits for the carry to ripple through sixty-three others, like a long line of dominoes. The fix: look at each column ahead of time. If both bits are one, that column will generate a carry, no matter what comes in. If exactly one bit is one, it will propagate a carry: it passes along whatever arrives.

Take five plus three again. The rightmost column is one and one: it generates. The next two columns each have a single one: they propagate. The last column has neither.

With those two signals, a carry-lookahead circuit works out every carry at the same time, without waiting.

For example, column three gets a carry because column zero generates, and columns one and two both propagate. A few AND and OR gates check that directly, for every column at once.

Real processors use tricks like this to add sixty-four bits in a tiny fraction of a nanosecond.

So: multiplying is shifting and adding. Dividing is shifting and subtracting. And clever wiring makes adding fast. Next time: numbers that aren't whole. Why does zero point one plus zero point two not quite equal zero point three? See you there.

Next episode: why 0.1 + 0.2 doesn't quite equal 0.3. Floating-point numbers.

Buy the Developer a Coffee

Entirely up to you: it works the same either way.

Every cup turns into the next update.

WeChat Pay QR code
WeChat Pay
Alipay QR code
Alipay
PayPal QR code
PayPal