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

One Circuit, Four Answers: Building an ALU From Logic Gates

Pressing play loads the video from YouTube.

Two numbers go in, five and one. Turn a small dial and the same circuit answers six, four, one or five. How?
We run AND and OR across whole rows of bits, build a multiplexer that picks one signal out of several, meet its cousin the decoder, see how a computer compares two numbers with one subtraction and a few flag lights, and put it all together into an ALU, the arithmetic logic unit.

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

Transcript

Two numbers go in: five and one. Watch the answer. Add: six. Subtract: four. AND: one. OR: five. Same two numbers, four different answers. The only thing that changed is this little dial. Today, we build the circuit behind it.

So far we have an adder that can also subtract, with one control line deciding which. But a computer needs more jobs than that. It needs AND and OR on whole rows of bits, and it needs to compare numbers.

AND on a row of bits is easy: four AND gates side by side, one for each column. Five is zero, one, zero, one. One is zero, zero, zero, one. Column by column, AND gives zero, zero, zero, one. Swap in four OR gates, and the same inputs give zero, one, zero, one. So now we have three circuits, all looking at the same two numbers, each with its own answer. We need a way to pick one.

Picture a railway switch. Two tracks come in, one goes out, and a lever decides which track gets through. We can build the same thing for a signal. It's called a multiplexer, or mux for short. Two inputs, A and B. One output. And a select line, S. When S is zero, the output copies A. When S is one, it copies B. Inside, it's just the gates we know. One AND gate lets A through only when S is zero, thanks to a NOT gate on S. Another AND gate lets B through only when S is one. An OR gate joins the two paths. Since only one path is open at a time, whatever comes out is the input we picked. To choose from four inputs, stack the switches: two muxes pick within each pair, and a third picks between the pairs. Now it takes two select lines. Two bits make four combinations: zero zero, zero one, one zero, one one. One for each input.

There's a close cousin of the mux, called a decoder. It goes the other way: two input bits, four output lines. Exactly one output lights up: the one whose number you typed in. Zero zero lights line zero. One one lights line three. Each output is one AND gate, with NOT gates on the inputs it wants to be zero. Keep the decoder in mind. In a few episodes, it will pick one cell out of millions in memory.

Now, comparing. Is five equal to one? Is it bigger? We don't need a new circuit for that. We subtract. Five minus one is four. Not zero, so they're not equal. And the answer isn't negative, so five is the bigger one, as long as nothing overflowed. To check for zero, feed all four result bits into a single NOR gate: an OR gate with a NOT on its output. It gives one only when every bit is zero. That light is called the zero flag. The leftmost bit of the result tells us the sign: that's the negative flag. Together with the carry and overflow flags from last time, these little lights describe the answer: zero or not, negative or not, too big or not. Subtract, then look at the flags. That's how a computer compares.

Time to put it all together. A and B go into three units at once: the adder and subtractor, the row of ANDs, and the row of ORs. All three work at the same time, every time. Each produces its own answer. A four-way mux on each column picks which answer comes out. Its two select lines are the operation code. Zero zero means add. Zero one means subtract: the same bit also drives the subtract line. One zero means AND. One one means OR. And the flags sit on the side, describing whatever came out. Engineers draw this whole thing as one shape, with a notch on top: the ALU, the arithmetic logic unit. Real ALUs have more operations: shifting bits, X-OR, and more. But the idea is exactly this: every unit computes, and a mux picks the answer. Back to our dial. Five and one. Add: six. Subtract: four. AND: one. OR: five.

Our ALU answers instantly, but it has no memory. Change the inputs, and the old answer is gone. To build a computer, we also need circuits that remember. We'll build those soon. But next time, our ALU learns to multiply and divide. See you there.

Next episode: how the same parts multiply and divide.

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