# How Computers Calculate – the ALU: Crash Course Computer Science #5

Hi, I’m Carrie Ann and this is Crash Course Computer Science. So last episode, we talked about how numbers can be represented in binary.
Representing Like, 00101010 is 42 in decimal. Representing and storing numbers is an important function of a computer, but the real goal is computation, or manipulating numbers in a structured and purposeful way, like adding two numbers together. These operations are handled by a computer’s Arithmetic and Logic Unit, but most people call it by its street name: the ALU. The ALU is the mathematical brain of a computer. When you understand an ALU’s design and function, you’ll understand a fundamental part of modern computers. It is THE thing
that does all of the computation in a computer, so basically everything uses it. First though, look at this beauty. This is perhaps the most famous ALU ever, the Intel 74181. When it was released in 1970, it was It was the first complete ALU that fit entirely inside of a single chip – Which was a huge engineering feat at the time. So today we’re going to take those Boolean logic gates we learned about last week to build a simple ALU circuit with much of the same functionality as the 74181. And over the next few episodes we’ll use this to construct a computer from scratch. So it’s going to get a little bit complicated, but I think you guys can handle it. INTRO An ALU is really two units in one — there’s
an arithmetic unit and a logic unit. Let’s start with the arithmetic unit, which is responsible for handling all numerical operations in a computer, like addition and subtraction. It
also does a bunch of other simple things like add one to a number, which is called an increment operation, but we’ll talk about those later. Today, we’re going to focus on the pièce de résistance, the crème de la crème of operations that underlies almost everything else a computer does – adding two numbers together. We could build this circuit entirely out of individual transistors, but that would get
confusing really fast. So instead as we talked about in Episode 3 – we can use a high-level of abstraction and build our components out of logic gates, in this case: AND, OR,
NOT and XOR gates. The simplest adding circuit that we can build takes two binary digits, and adds them together. So we have two inputs, A and B, and one output, which is the sum of those two digits. Just to clarify: A, B and the output are all single bits. There are only four possible input combinations. The first three are:
0+0=0 1+0=1
0+1=1 Remember that in binary, 1 is the same as
true, and 0 is the same as false. So this set of inputs exactly matches the boolean logic of an XOR gate, and we can use it as our 1-bit adder. But the fourth input combination, 1 + 1,
is a special case. 1 + 1 is 2 (obviously) but there’s no 2 digit in binary, so as we talked about last episode, the result is 0 and the 1 is carried to the next column. So the sum is really 10 in binary. Now, the output of our XOR gate is partially correct – 1 plus 1, outputs 0. But, we need an extra output wire for that carry bit. The carry bit is only “true” when the inputs are 1 AND 1, because that’s the only time when the result (two) is bigger than
1 bit can store… and conveniently we have a gate for that! An AND gate, which is
only true when both inputs are true, so we’ll add that to our circuit too. And that’s it. This circuit is called a half adder. It’s It’s not that complicated – just two logic gates – but let’s abstract away even this level of detail and encapsulate our newly minted half adder as its own component, with two inputs – bits A and B – and two outputs, the sum and the carry bits. This takes us to another level of abstraction… heh… I feel like I say that a lot. I wonder if this is going to become a thing. Anyway, If you want to add more than 1 + 1 we’re going to need a “Full Adder.” That half-adder left us with a carry bit as output. That means that when we move
on to the next column in a multi-column addition, and every column after that, we are going to have to add three bits together, no two. A full adder is a bit more complicated – it takes three bits as inputs: A, B and C. So
the maximum possible input is 1 + 1 + 1, which equals 1 carry out 1, so we still
only need two output wires: sum and carry. We can build a full adder using half adders. To do this, we use a half adder to add A plus B just like before – but then feed that
result and input C into a second half adder. Lastly, we need a OR gate to check if either one of the carry bits was true. That’s it, we just made a full adder! Again,we can go up a level of abstraction and wrap up this full adder as its own component. It
takes three inputs, adds them, and outputs the sum and the carry, if there is one. Armed with our new components, we can now build a circuit that takes two, 8-bit numbers – Let’s call them A and B – and adds them together. Let’s start with the very first bit of A and B, which we’ll call A0 and B0. At
this point, there is no carry bit to deal with, because this is our first addition.
So we can use our half adder to add these two bits together. The output is sum0.
Now we want to add A1 and B1 together. It’s possible there was a carry from the previous addition of A0 and B0, so this time we need to use a full adder that also inputs the carry
bit. We output this result as sum1. Then, we take any carry from this full adder, and run it into the next full adder that handles A2 and B2. And we just keep doing this in
a big chain until all 8 bits have been added. Notice how the carry bits ripple forward to
this is how many simple processors, like those in your thermostat, TV remote, and microwave, do multiplication. It’s slow, but it gets the job done. However, fancier processors, like those in your laptop or smartphone, have arithmetic units with dedicated circuits for multiplication. And as you might expect, the circuit is more complicated than addition — there’s no magic, it just takes a lot more logic gates
– which is why less expensive processors don’t have this feature. Ok, let’s move on to the other half of the ALU: the Logic Unit. Instead of arithmetic
operations, the Logic Unit performs… well… logical operations, like AND, OR and NOT, which we’ve talked about previously. It also performs simple numerical tests, like checking if a number is negative. For example, here’s a circuit that tests
if the output of the ALU is zero. It does this using a bunch of OR gates to see if any of the bits are 1. Even 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 input so the output is 1 only if the input number is 0. So that’s a high level overview of what makes up an ALU. We even built several of the main components from scratch, like our ripple adder. As you saw, it’s just a big bunch of logic gates connected in clever ways. Which brings us back to that ALU you admired so much at the beginning of the episode. The Intel 74181. Unlike the 8-bit ALU we made today, the 74181 could only handle 4-bit inputs, which means YOU BUILT AN ALU THAT’S LIKE TWICE AS GOOD AS THAT SUPER FAMOUS ONE. WITH
YOUR MIND! Well.. sort of. We didn’t build the whole thing… but you get the idea. The 74181 used about 70 logic gates, and it couldn’t multiply or divide. But it was a huge step forward in miniaturization, opening the doors to more capable and less expensive computers. This 4-bit ALU circuit is already a lot to take in, but our 8-bit ALU would require hundreds of logic gates to fully build and engineers don’t want to see all that complexity when using an ALU, so they came up with a special symbol to wrap it all up, which looks like
a big ‘V’. Just another level of abstraction! Our 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, for example, addition or subtraction. For that, we use a 4-bit operation code. We’ll talk about this more in a later episode, but in brief, 1000 might be the command to add, while 1100 is the command for subtract. Basically, the operation code tells the ALU what operation to perform. And the result of that operation on inputs A and B is an 8-bit output. ALUs also output a series of Flags, which are 1-bit outputs for particular states and statuses. For example, if we subtract two numbers, and the result is 0, our zero-testing circuit, the one we made earlier, sets the Zero Flag to True (1). This is useful if we are trying to determine if two numbers are are equal. If we wanted to test if A was less than B, we can use the ALU to calculate A subtract B and look to see if the Negative Flag was set to true. If it was, we know that A was
smaller than B. And finally, there’s also a wire attached to the carry out on the adder we built, so if there is an overflow, we’ll know about it. This is called the Overflow Flag. Fancier ALUs will have more flags, but these three flags are universal and frequently used. In fact, we’ll be using them soon in a future episode. So now you know how your computer does all its basic mathematical operations digitally with no gears or levers required. We’re going to use this ALU when we construct our CPU two episodes from now. But before that, our computer is going to need some memory! We’ll talk about that next week.

Tags: , , , , , , , , , , , , , , , , , , , , , ,

## 100 thoughts on “How Computers Calculate – the ALU: Crash Course Computer Science #5”

1. TheDistortion says:

MISTAKE!
The computer computes fast not because of electrons are moving fast, it's due to the fast transmitting electric field!
In fact, electrons move quite slow in circuits generally.

2. Teo Extreme says:

You did not explane how to make a ''divider'' using logic gates.

3. Cool Cooler says:

I'm impressed! Such a delightful, easy to understand, deserving to be recommended to everyone computer science course! Nailed it!

4. 4C1D_8URN2 says:

This perfect, thank you! Computers are not that complicated if you understand transistors, it only gets complicated when there are a million of them.

5. Gumbo Clay says:

How do computers do multiplication with repeated addition? Do they use the increment function to keep track of the number of times the addition is performed?

6. pan Pan says:

真的很棒这个 谢谢主持人

7. Simone Favaro says:

gerosa gang reportin in

8. Siddhi Sharma says:

At 7:05, she said it takes 5 passes for 12*5. Shouldn't it be 4 passes, as 1st 12+12, 2nd 24+12 (if its done that way) ..all the way to 60?

9. Tolgay Atamtürk says:

OMG thank you, i didn't get this at all in class, but here it is explained so well!!!

10. Joshua says:

I feel like my brain wants me to think this is confusing but at the same time I actually get it.

11. meisterlampe says:

Geek question: since subtraction is implemented via the two's complement representation, one basically uses the overflow whenever a subtraction results in a positive value. How come an ALU can detect an overflow then? You would need extra logic for sanity checking the inputs, wouldn't you?

12. Gregory Fenn says:

Maybe it's my way of learning, but I find computer science easier if I start high-level abstract and then work my way lower. [This is also how I learned to code, first Python, then Java, then C#, then C++/C]

13. 김형준 says:

This is the best course i have taken in my entire life

14. Steven Bowman says:

Wow I understand computers on a level I never anticipated. I've been following along, taking notes then explaining the concepts to my wife when she gets home. She isn't quite as excited as I am though XD

15. WESTECH MEDIA says:

ALU – the street name! haha

16. photag216 says:

Uuuuuh….what?

17. Jovana Pavlovic says:

Thanks so much, Ur amazing! ❤️

18. serine ch says:

her way of explining thingsmakes it look that its so simple and everyone could get it thank u i am really impressed

19. Joshuel Simbulan says:

There is no 1 0 1 in the full adder table…

wow im impressed

21. camgere says:

Briefly The Kinks and Steely Dan were right. Do It Again. Logic starts out with Small Scale Integration (SSI). A bunch of NAND gates limited by the number of pins on the package. As technology improves we go to Medium Scale Integration (MSI). An 8 bit counter for instance. Technology improves some more and we go to Large Scale Integration (LSI). Then VLSI. Then a totally new technology comes out in SSI. Then goes to MSI, LSI and VLSI. ALU's may seem old hat, but with Field Programmable Gate Arrays and other programmable logic, they become core components in a library. All of sudden every minute detail of this design is important again. A newer technology will then come along. Do It Again.

22. DD O Befaest says:

First off, this is great. Coming back after two years because I had to start intensive work since this first came out and couldn't wait for the episodes one at a time.

I have to say at this point, you first drop the ball. After taking us step by step in precise detail about how each individual, fundamental step works on the construction, you suddenly zoom past the full adder part without taking the time to outline how it works in detail or show examples to demonstrate. Also, I think it helps to overstate the significance of xor gates essentially being single bit binary adders. To me, that helps acknowledge where that actual adding process is occurring in the circuit.
You move up the level of abstraction too quickly here. Also, I know you come back to it after, but it's really not initially apparent where the first C input comes from in the full adder.

v=VPw9vPN-3ac maybe this will help people if they want to go through it again slowly before moving past the full adder and actually understanding it.

Remember A**ange m/

23. Barron Fritz says:

Your fact about Pacman becoming unplayable after level 255 is incorrect. It's not because the level rolls over, it's because the number of fruits displayed rolls over. In fact, the bug occurred on level 255, not after. There are hacked patches that fix this bug

24. Tilak Rijal says:

????

25. Taqqi Raja says:

THIS WAS AWESOME!!!!!!!!!!!

26. Joanne Msk says:

Почему нет русских субтитров? Очень жаль…😢 Просто мой английский оставляет желать лучшего, а в русском Ютубе некачественная инфа.

27. Andrew Cekuta says:

great videos!

28. Hande Toffoli says:

Great series!!!! Love the presentation and the content.

29. t r a g i c s i s says:

I wanna cry, I have an exam tomorrow on IT and I have this

🅱️oolean

31. Phased Spaces says:

This takes me back to my late 80s computer science 'A' level. 😀

32. Anders Jikishin Larsen says:

It's not the electrons that move very fast (in electronics), it's the electrical energy. Electrons actually move quite slow. Approximately 1.5 cm / 0.6 inches per minute in copper wires for example.

33. man of the woods says:

Street name huh? Lol

34. suraj dev says:

I love you mam.

35. Juan De Souza says:

The quality of these videos is just absurd. it's difficult to believe how it was possible to turn such an abstract and theoretic content into fun animations and images.

36. Berna says:

Everything according to money is not the way

37. Dimitar Dimitrov says:

what is the bottom most right symbol for? it has two circles before the OR???

38. Kevin Funk says:

6:59 Correction *4 times.

39. Jamshid Ergashov says:

Its amazing, good job Carrie Ann!

40. Samim Rahman says:

Excellent tutorial Carri Ann. Though I'm not a Computer Engineer, but I've passion on Computer science. I often Study the various topics on computer science.
Thanks.

41. typograf62 says:

Sadly that particular original famous one-chip ALU at 8:15 is made by Texas Instruments.

42. Kayden 54566 says:

Actually I did make a ALU but in Minecraft

43. Victor Bezak says:

In any scientific endeavor–especially computer science–it's a positive sign if you're feeling overwhelmed. That means you're trying. The only way to become an expert is to allow yourself to be constantly overwhelmed and to continue persevering. If you invest enough time and effort, all of a sudden you'll realize that things start clicking and making sense. No matter how difficult a concept seems, or how slowly you feel like you're learning, just don't give up, keep coming back, and one day you'll have mastered what you once thought was too difficult for you to learn. You can do it.

44. Akshhat Srivastava says:

This is so much better than the digital electronics course offered at my college.

45. Tim Allen says:

Bhai i need all the calculating devices names in sequence first to last

47. Sean D'Souza says:

In the full adder table, you are missing the 1 0 1 permutation.

48. xamarmm says:

A few inaccuracies though. Overflow bit is not the same as the carry bit. Carry bit is UNSIGNED overflow, while the overflow bit is for SIGNED overflow. They are distinct. Essentially, if you add two unsigned numbers and want to see if they overflow, check the carry bit, if you add two signed numbers and want to see if they overflow, check the overflow bit. This is true for most computers out there even if they name the bits differently although I believe the carry bit is universally called the carry bit in all computers that I know of.

49. 석영미 says:

やばすぎる!!!

50. Carl L.D. says:

While abstraction is not a wrong term, the more precise and contextual one is encapsulation which refer to the black box concept of machinery. 🙂

51. Guma Nelson says:

If you were me and I was you, you wouldn't understand me even a bit.

52. Domitori P. says:

Never saw a person that got so excited because of logic gates

53. Navin T says:

Crash course is awesome. Cant wait to watch rest of this series..

54. AlexATG says:

Watching this while try to build one in mc LOL

55. Braxton Brown says:

Just building this in minecraft now…

56. Gourav Sarkar says:

[ insert mind here ]

57. Mutian Wang says:

I'm a software engineer and I think this is a great course series, very helpful

58. Michael Novello says:

I wish these videos were a little slower. They talk so quickly, and slowing the video down makes the sound distorted. It would be no matter for people who want to speed it up to just speed it up without distorting anything. Otherwise this video would get a thumbs up from me.

59. Waqas Narang says:

This Video is GOLD. It should have been viewed billions of times. But then i think, the real number is always less.

60. TheOriginalJoeBloggs says:

More girls should be like you 😉

61. david jaki says:

the street name of the ALU now is "the foxi"

62. Dapper Don says:

why cant computers just use the decimal system. this seems so complicated for no reason. like a temporary hotfix. can someone explain? imean cant we use different voltages for representing numbers 0-9?

63. gdenn says:

amazing video

64. Saurabhav says:

I was following along perfectly fine till this video lol

65. miguelfinland says:

Nice video, good content and teacher is very clever and nice (thanks 🙂 but, like so often in nowdays, too fast speaking. Why is it so common in teaching videos today? Tip: It is not bad if you remember breathing and stops between thr sentences, and finally – we will not leave you if you give us short breaks. Stop running if you can walk! 🙂

66. Tomáš Heller says:

waoo! What a great approaches for me as an apprentice learner. I am really impressed. Thanks for the nice contents for us.

68. markdelej says:

I am a biological computer which cannot understand how computers work, mind blown 🤯

69. Justin McCown says:

CREEPA

70. Patrick Kim says:

still bullish on COVA scammer???

71. Patrick Kim says:

any more excuse now scammer? long right? lol

72. Patrick Kim says:

flip a coin and gamble with your mom's money, stop losing client money on leverage scammer

73. Patrick Kim says:

four, five, seven, 10-20 times!! your range is so fake LOL. just like u bought bitcoin 5, 8, 9 years ago. just make up and exxaggerate as you go Rob!!!

74. Patrick Kim says:

u hold on to that "may drop" for so long. right after you call a 10x….. SILENT about that call.

75. Patrick Kim says:

anyone can make 10 predicitons….. stay silent mostly, and BAM when one is remotely close to what you mentioned. thats ur TA???

76. Patrick Kim says:

"i'm a dweeb" "BAM" "i'm a lil bit**" "BAM" ur such a fat little GURL

77. Patrick Kim says:

GOT FATTER bobby? not showing ur face anymore? eating all that instant cup millioniare???

78. Patrick Kim says:

u bought bitcoin 9 years ago in… 2010 right bob??? right around the time you were doing PLASTIC MOLDING LOL

79. Patrick Kim says:

ITS SO pathetic how you hang on to every remote win. u have so few of those. "1 bad cal, 3 correct calls!!" (praying for customers) wen car payment???

80. Patrick Kim says:

u get 1 call remotely correctly, 50 bams! 1 bad call??? SILENCE lol

81. Patrick Kim says:

ur like a lonely housewife that nags her husband on the side. "see, look" "bam" "i told u so" "u will need to follow us" "im gonna need a bam" u little cu**

82. Patrick Kim says:

cool, calm, collected to "fu** off liar!" LOL

83. Patrick Kim says:

hows that 10x taste FUND MANAGER LOL

84. Patrick Kim says:

clients complaining about losses. thats all i see. never ever see clients saying they made money with you….. always complaining of being scammed and 10%, 20%, 80% losses!! LOL!!!! such big range master~~~ LOL

85. Patrick Kim says:

86. Patrick Kim says:

time to meditate again scamming bobby!

87. Javier R says:

The image at 8:20 of the Intel 74181 seems to be a Texas Instruments chip.

88. Cubebass says:

you know, I'm actually taking notes on this playlist.

89. Bipin Panday says:

can you bind all the video series into one playlist?

90. Bryan Gui says:

ALU made in Malaysia… woo hoo!
We were once the world largest producer of semiconductors before China took over.

91. azrul nizam says:

So all of this crazy transistors setup are added to the instruction set dictionary. How many thousand of words that they have created through x86. Are all of theme being used as often as the other? I wonder.

92. Mazapan Putrefacto says:

Damn as someone whos seeing this for the first time its really overwhelming, kinda discouraged to keep on learning at this point lol

93. Humanitarian WATSON says:

Are you telling a story or teaching ? I'm confused

94. Taripar says:

I kinda wish this video had gone into more detail on the circuitry for the opcodes, as they're hardwired individually for each CPU. I would've liked to see a schematic for how the opcode bits interact with the ALU (i.e. exactly HOW the opcode 1000 causes the chain reaction of switches to add the two 8-bit numbers vs. subtract/increment/etc.), as well as how the ALU calculates the negative flag (the Check Zero flag was mentioned and the carry bit is clearly the Overflow flag, but the negative one confuses me).

Perhaps this will be answered in part later on, but I wanted more info on that bit (HA) specifically.

95. christian37ism says:

Informative and thorough video, thank you for this

96. Smokey Brown says:

Aww man creeper

97. 01001100 IVE says:

Me: It's simple as 1+1!
ALU: Is it really?

98. דוד כהן says:

התרגום בעברית טוב אבל לא מושלם

99. Amanda Purello says:

aahhhh my brain is melting lol

100. Zhaun Fouche says:

That moment when you explain the '8bit ripple carry adder' it comes across as an "IF" formula within Microsoft Excel. Where you would use other formula within the "IF" to gain a specified response if the outcome were in fact true or false eg. =IF(A1=1,True,False) or further eg. =IF(A1=1,True,IF(B1=1,True,False)), using only 0 and 1 within the example.