How Computers Calculate - the ALU: Crash Course Computer Science #5 — Transcript
Full transcript
- 0:03Hi, I’m Carrie Ann and this is Crash Course Computer Science.
- 0:05So last episode, we talked about how numbers can be represented in binary. Representing
- 0:09Like, 00101010 is 42 in decimal.
- 0:13Representing and storing numbers is an important function of a computer, but the real goal is computation,
- 0:19or manipulating numbers in a structured and purposeful way, like adding two numbers together.
- 0:23These operations are handled by a computer’s Arithmetic and Logic Unit,
- 0:27but most people call it by its street name: the ALU.
- 0:29The ALU is the mathematical brain of a computer.
- 0:32When you understand an ALU’s design and function, you’ll understand a fundamental
- 0:36part of modern computers. It is THE thing that does all of the computation in a computer,
- 0:41so basically everything uses it.
- 0:43First though, look at this beauty.
- 0:45This is perhaps the most famous ALU ever, the Intel 74181.
- 0:50When it was released in 1970, it was
- 0:52It was the first complete ALU that fit entirely inside of a single chip -
- 0:57Which was a huge engineering feat at the time.
- 0:59So today we’re going to take those Boolean logic gates we learned about last week
- 1:03to build a simple ALU circuit with much of the same functionality as the 74181.
- 1:08And over the next few episodes we’ll use
- 1:10this to construct a computer from scratch. So it’s going to get a little bit complicated,
- 1:14but I think you guys can handle it.
- 1:16INTRO
- 1:25An ALU is really two units in one -- there’s an arithmetic unit and a logic unit.
- 1:30Let's start with the arithmetic unit, which is responsible for handling all numerical operations in a
- 1:34computer, like addition and subtraction. It also does a bunch of other simple things like
- 1:39add one to a number, which is called an increment operation, but we’ll talk about those later.
- 1:43Today, we’re going to focus on the pièce de résistance, the crème de la crème of
- 1:47operations that underlies almost everything else a computer does - adding two numbers together.
- 1:52We could build this circuit entirely out of
- 1:54individual transistors, but that would get confusing really fast.
- 1:57So instead as we talked about in Episode 3 – we can use a high-level of abstraction and build our components
- 2:03out of logic gates, in this case: AND, OR, NOT and XOR gates.
- 2:08The simplest adding circuit that we can build takes two binary digits, and adds them together.
- 2:12So we have two inputs, A and B, and one output, which is the sum of those two digits.
- 2:18Just to clarify: A, B and the output are all single bits.
- 2:21There are only four possible input combinations.
- 2:24The first three are: 0+0 = 0
- 2:271+0 = 1 0+1 = 1
- 2:30Remember that in binary, 1 is the same as true, and 0 is the same as false.
- 2:35So this set of inputs exactly matches the boolean logic of an XOR gate, and we can use it as
- 2:39our 1-bit adder.
- 2:40But the fourth input combination, 1 + 1, is a special case. 1 + 1 is 2 (obviously)
- 2:46but there’s no 2 digit in binary, so as we talked about last episode, the result is
- 2:500 and the 1 is carried to the next column. So the sum is really 10 in binary.
- 2:54Now, the output of our XOR gate is partially correct - 1 plus 1, outputs 0.
- 3:00But, we need an extra output wire for that carry bit.
- 3:03The carry bit is only “true” when the inputs are 1 AND 1, because that's the only
- 3:06time when the result (two) is bigger than 1 bit can store… and conveniently we have
- 3:10a gate for that! An AND gate, which is only true when both inputs are true, so
- 3:15we’ll add that to our circuit too.
- 3:17And that's it. This circuit is called a half adder. It’s
- 3:19It's not that complicated - just two logic gates - but let’s abstract away even this level
- 3:24of detail and encapsulate our newly minted half adder as its own component, with two
- 3:28inputs - bits A and B - and two outputs, the sum and the carry bits.
- 3:32This takes us to another level of abstraction… heh… I feel like I say that a lot.
- 3:36I wonder if this is going to become a thing.
- 3:43Anyway, If you want to add more than 1 + 1
- 3:46we’re going to need a “Full Adder.” That half-adder left us with a carry bit as output.
- 3:50That means that when we move on to the next column in a multi-column addition,
- 3:54and every column after that, we are going to have to add three bits together, no two.
- 3:59A full adder is a bit more complicated - it
- 4:00takes three bits as inputs: A, B and C. So the maximum possible input is 1 + 1 + 1,
- 4:07which equals 1 carry out 1, so we still only need two output wires: sum and carry.
- 4:12We can build a full adder using half adders. To do this, we use a half adder to add A plus B
- 4:17just like before – but then feed that result and input C into a second half adder.
- 4:22Lastly, we need a OR gate to check if either one of the carry bits was true.
- 4:27That’s it, we just made a full adder! Again,we can go up a level of abstraction and wrap
- 4:31up this full adder as its own component. It takes three inputs, adds them, and outputs
- 4:36the sum and the carry, if there is one.
- 4:38Armed with our new components, we can now build a circuit that takes two, 8-bit numbers
- 4:42– Let’s call them A and B – and adds them together.
- 4:44Let’s start with the very first bit of
- 4:46A and B, which we’ll call A0 and B0. At this point, there is no carry bit to deal
- 4:51with, because this is our first addition. So we can use our half adder to add these
- 4:55two bits together. The output is sum0. Now we want to add A1 and B1 together.
- 5:01It's possible there was a carry from the previous addition of A0 and B0, so this time we need
- 5:06to use a full adder that also inputs the carry bit. We output this result as sum1.
- 5:11Then, we take any carry from this full adder, and run it into the next full adder that handles
- 5:16A2 and B2. And we just keep doing this in a big chain until all 8 bits have been added.
- 5:21Notice how the carry bits ripple forward to each subsequent adder. For this reason,
- 5:26this is called an 8-bit ripple carry adder. Notice how our last full adder has a carry out.
- 5:32If there is a carry into the 9th bit, it means the sum of the two numbers is too large to fit into 8-bits.
- 5:36This is called an overflow.
- 5:37In general, an overflow occurs when the result of an addition is too large to be represented by the number of bits you are using.
- 5:43This can usually cause errors and unexpected behavior.
- 5:45Famously, the original PacMan arcade game used 8 bits to keep track of what level you were on.
- 5:50This meant that if you made it past level 255 – the largest number storablein 8 bits –
- 5:55to level 256, the ALU overflowed.
- 5:58This caused a bunch of errors and glitches making the level unbeatable.
- 6:01The bug became a rite of passage for the greatest PacMan players.
- 6:04So if we want to avoid overflows, we can extend our circuit with more full adders, allowing
- 6:09us to add 16 or 32 bit numbers. This makes overflows less likely to happen, but at the
- 6:14expense of more gates. An additional downside is that it takes a little bit of time for
- 6:19each of the carries to ripple forward.
- 6:21Admittedly, not very much time, electrons move pretty fast, so we’re talking about billionths of a second,
- 6:26but that’s enough to make a difference in today’s fast computers.
- 6:29For this reason, modern computers use a slightly different adding circuit called a ‘carry-look-ahead’ adder
- 6:35which is faster, but ultimately does exactly the same thing-- adds binary numbers.
- 6:39The ALU’s arithmetic unit also has circuits for other math operations
- 6:43and in general these 8 operations are always supported.
- 6:46And like our adder, these other operations are built from individual logic gates.
- 6:50Interestingly, you may have noticed that there are no multiply and divide operations.
- 6:54That's because simple ALUs don’t have a circuit for this, and instead just perform a series of additions.
- 6:59Let’s say you want to multiply 12 by 5.
- 7:01That’s the same thing as adding 12 to itself 5 times. So it would take 5 passes through
- 7:06the ALU to do this one multiplication. And this is how many simple processors,
- 7:10like those in your thermostat, TV remote, and microwave, do multiplication.
- 7:15It’s slow, but it gets the job done.
- 7:17However, fancier processors, like those in your laptop or smartphone,
- 7:20have arithmetic units with dedicated circuits for multiplication.
- 7:23And as you might expect, the circuit is more complicated than addition -- there’s no
- 7:27magic, it just takes a lot more logic gates – which is why less expensive processors
- 7:31don’t have this feature.
- 7:32Ok, let’s move on to the other half of the ALU: the Logic Unit.
- 7:36Instead of arithmetic operations, the Logic Unit performs… well...
- 7:39logical operations, like AND, OR and NOT, which we’ve talked about previously.
- 7:44It also performs simple numerical tests, like checking if a number is negative.
- 7:47For example, here’s a circuit that tests if the output of the ALU is zero.
- 7:51It does this using a bunch of OR gates to see if any of the bits are 1.
- 7:55Even if one single bit is 1, we know the number can’t be zero and then we use a final NOT gate to flip this
- 8:01input so the output is 1 only if the input number is 0.
- 8:05So that’s a high level overview of what makes up an ALU. We even built several of
- 8:09the main components from scratch, like our ripple adder.
- 8:11As you saw, it’s just a big bunch of logic gates connected in clever ways.
- 8:14Which brings us back to that ALU you admired so much at the beginning of the episode.
- 8:18The Intel 74181.
- 8:21Unlike the 8-bit ALU we made today, the 74181 could only handle 4-bit inputs,
- 8:26which means YOU BUILT AN ALU THAT’S LIKE
- 8:29TWICE AS GOOD AS THAT SUPER FAMOUS ONE. WITH YOUR MIND! Well.. sort of.
- 8:34We didn’t build the whole thing… but you get the idea.
- 8:36The 74181 used about 70 logic gates, and it couldn’t multiply or divide.
- 8:41But it was a huge step forward in miniaturization, opening the doors to more capable and less expensive computers.
- 8:47This 4-bit ALU circuit is already a lot to take in,
- 8:50but our 8-bit ALU would require hundreds of logic gates to fully build and engineers
- 8:54don’t want to see all that complexity when using an ALU, so they came up with a special
- 8:59symbol to wrap it all up, which looks like a big ‘V’. Just another level of abstraction!
- 9:09Our 8-bit ALU has two inputs, A and B, each with 8 bits. We also need a way to specify what operation the ALU should perform,
- 9:17for example, addition or subtraction.
- 9:19For that, we use a 4-bit operation code.
- 9:21We’ll talk about this more in a later episode, but in brief, 1000 might be the command
- 9:27to add, while 1100 is the command for subtract. Basically, the operation code tells the ALU
- 9:33what operation to perform. And the result of that operation on inputs A and B is an 8-bit output.
- 9:38ALUs also output a series of Flags, which are 1-bit outputs for particular states and statuses.
- 9:43For example, if we subtract two numbers, and the result is 0, our zero-testing circuit, the one we made earlier,
- 9:50sets the Zero Flag to True (1). This is useful if we are trying to determine if two numbers are are equal.
- 9:55If we wanted to test if A was less than B,
- 9:57we can use the ALU to calculate A subtract B and look to see if the Negative Flag was set to true.
- 10:03If it was, we know that A was smaller than B.
- 10:05And finally, there’s also a wire attached to the carry out on the adder we built,
- 10:09so if there is an overflow, we’ll know about it. This is called the Overflow Flag.
- 10:13Fancier ALUs will have more flags, but these three flags are universal and frequently used.
- 10:18In fact, we’ll be using them soon in a future episode.
- 10:21So now you know how your computer does all its basic mathematical operations digitally
- 10:25with no gears or levers required.
- 10:27We’re going to use this ALU when we construct our CPU two episodes from now.
- 10:31But before that, our computer is going to need some memory! We'll talk about that next week.
About this transcript
This page contains the full transcript of How Computers Calculate - the ALU: Crash Course Computer Science #5 by CrashCourse, generated from the public captions YouTube serves with the video. The transcript has 2,159 words across 156 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.
What you can do with it
Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to choose a caption track available on the video.
Free YouTube transcript tool
YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, caption language selection, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.