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

How 8 Switches Add Two Numbers

Pressing play loads the video from YouTube.

Eight switches, five lights, and no chip at all: by the end of this video you'll know exactly how they add two numbers.
We start with one switch and one light, build AND, OR and NOT gates, learn to count in binary, and put together a half adder, a full adder and a 4-bit adder.

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

Transcript

These eight switches can add two numbers. Five, and three. Flip, flip, flip, flip... and the lights say eight. No chip. No code. Just switches, and a box we will open at the end. By the end of this video, you'll know exactly what's inside it.

Let's start with the simplest circuit there is: a battery, a wire, a light, and a switch. Close the switch, and current flows all the way around. The light comes on. We'll call that one. Open the switch, and the flow stops. The light goes off. That's zero. One switch can only say two things: on or off, one or zero. That single yes-or-no is called a bit. It's the smallest piece of information a computer can hold. Every photo, every word, every game on a computer is built from bits like this one.

Now let's add a second switch, in a row with the first. We'll call them A and B. Close only A... nothing. Only B... still nothing. Close both, and the light comes on. Current has to get through A and then through B. If either one is open, the path is broken. Let's write all four cases in a table. Zero and zero gives zero. Zero and one, zero. One and zero, zero. One and one gives one. This is an AND gate. The output is one only when A and B are both one.

Now put the two switches side by side, so current has two roads to the light. Close A, and the light comes on. Close B instead, and it comes on too. Close both: still on. Only when both are open does the light go dark. This is an OR gate: the output is one when A is one, or B is one, or both.

One more. This button is wired the other way around: it's closed until you press it. So the light is on... press the button, and the light goes off. This is a NOT gate. Whatever goes in, the opposite comes out. One becomes zero, zero becomes one.

So far, we've been flipping these switches ourselves. In a real computer, nobody touches them. The switches are flipped by electricity. A transistor is a tiny switch that closes when a signal arrives on a third wire. That changes everything. Because now, the output of one gate can flip the switches of the next gate. Gates can drive other gates. And that means we can chain them together into bigger and bigger circuits. To keep the drawings simple, engineers give each gate its own symbol. This is AND, this is OR, and this little triangle with a circle is NOT. A modern chip packs billions of these switches, and each one can flip billions of times every second. But before we build anything big, we need to know how to count with them.

We count in tens. The right column counts ones. When it passes nine, it rolls back to zero and adds one to the next column: the tens. So two hundred fifty-three means two hundreds, five tens, and three ones. Now imagine you only have two digits: zero and one. That's all a switch can show. It works the same way. The right column counts ones. But it rolls over after one, not after nine. So each column is worth twice the one before it: one, two, four, eight. Let's count with four lights. Zero. One. Now the first column is full, so it carries: two is one, zero. Three is one, one. Four is one, zero, zero. Keep going, and four lights can count all the way from zero to fifteen. Five is four plus one: zero, one, zero, one. Three is two plus one: zero, zero, one, one. That's exactly what our switches were showing at the start. This is called binary.

Now, adding. Let's start with the smallest possible sum: one bit plus one bit. There are only four cases. Zero plus zero is zero. Zero plus one is one. One plus zero is one. And one plus one is two, which in binary is one, zero. We write down zero, and carry the one. So every one-bit sum has two outputs: the sum bit, and the carry bit. Look at the carry column: zero, zero, zero, one. We've seen that pattern before. It's an AND gate. And the sum column: zero, one, one, zero. It's one only when the two inputs are different.

We don't have a gate for that yet, but we can build one from the three we have. Take A OR B. That's one when at least one input is on. Then block the case where both are on, using AND and NOT. The result is one only when the inputs are different. This is called exclusive OR, X-OR for short, and it gets its own symbol. Put an XOR and an AND side by side, feed them the same two bits, and you get the sum and the carry. That's a half adder.

Why only half? Think about how you add bigger numbers on paper. You start on the right, add the column, and carry into the next one. So every column after the first has three numbers to add: A, B, and the carry coming in. A half adder only takes two. So we use two of them. The first half adder adds A and B. The second adds that result to the incoming carry. If either of them produced a carry, we pass a carry on. One OR gate takes care of that. Two half adders and an OR gate: that's a full adder. Three bits in, a sum and a carry out.

Now line up four full adders, one for each column, and connect each carry out to the next carry in. Remember the box from the beginning? Let's open it. This is what was inside all along: four full adders in a row. Five plus three again. The first column: one plus one. Sum zero, carry one. The carry moves left, like a baton in a relay race. Zero plus one, plus the carry: sum zero, carry one. And again in the next column. Until the last column: zero plus zero plus the carry gives one. The lights read zero, one, zero, zero, zero. That's eight. Real computers do the same thing with sixty-four columns instead of four. The idea doesn't change.

Let's look back. A switch gave us one bit. Switches in a row and side by side gave us gates. Gates flipping gates gave us an adder. Not once did we leave the world of on and off. But there's a problem. There's no minus sign anywhere in this circuit. So how does a computer subtract, or even store a negative number? That's the next episode. See you there.

Next episode: negative numbers, and how the same adder learns to subtract.

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