hi i'm carrie anne and this is crash
course computer science so last episode
we talked about how numbers can be
represented in binary like 0 0 1 0 1 0 1
0 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 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 the first
complete alu that fit entirely inside 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 that 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
[Music]
an alu is really two units in one
there's an arithmetic unit and a logic
cue lit 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 pistol resistance the creme de la
creme of operations that underlines
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 three 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 zero plus zero
equals zero one plus zero equals one and
zero plus one equals one remember that
in binary one is the same as true and
zero 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
one bit adder but the fourth input
combination one plus one is a special
case one plus one is two obviously but
there's no two digit in binary so as we
talked about last episode the result is
zero and the one is carried to the next
column so the sum is really one zero in
binary now the output of our xor gate is
partially correct one plus one output
zero but we need an extra output wire
for that carry bit the carry bit is only
true when the inputs are one and one
because that's the only time when the
result is bigger than one 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 two and that's it this circuit
is called a half adder 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 i feel like
i said that a lot i wonder if this is
going to become a thing
anyway if you want to add more than one
plus one 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're going to have to add
three bits together not 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 one plus one plus one
which equals one carry out one 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
an 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 add some
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 those two bits together the output
is sum zero now we want to add a1 and b1
together it's possible there was a carry
from the previous edition 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 sum one then we take any
carry from this four adder and run it
into the next full adder that handles a2
and b2 and we just keep doing this in a
big chain so all eight bits have been
added notice how the carry bits ripple
forward to each subsequent adder for
this reason this is called an 8 bit
ripple carry adder notice how our last 4
adder has a carry out if there is a
carry into the ninth bit it means the
sum of the two numbers is too large to
fit into 8 bits this is called an
overflow in general an overflow occurs
when the result of an addition is too
large to be represented by the number of
bits you are using this can usually
cause errors in unexpected behavior
famously the original pacman arcade game
used 8 bits to keep track of what level
you were on this meant that if you made
it past level 255 the largest number
storable in 8 bits to level 256 the alu
overflowed this caused a bunch of errors
and glitches making the level unbeatable
the bug became a right of passage for
the greatest pac-man players so if we
want to avoid overflows we can extend
our circuit with more full adders
allowing us to add 16 or 32-bit numbers
this makes overflows less likely to
happen but at the expense of more gates
an additional downside is that it takes
a little bit of time for each of the
carries to ripple forward admittedly not
very much time electrons move pretty
fast so we're talking about billionths
of a second that's enough to make a
difference in today's fast computers for
this reason modern computers use a
slightly different adding circuit called
a carry look ahead adder which is faster
but ultimately does exactly the same
thing adds binary numbers the alu's
arithmetic unit also has circuits for
other map operations and in general
these eight operations are always
supported and like our adder these other
operations are built from individual
logic gates interestingly you may have
noticed that there are no multiply and
divide operations that's because simple
alus don't have a circuit for this and
instead just perform a series of
additions let's say you want to multiply
12 by 5 that's the same thing as adding
12 to itself five times so it would take
five passes through the alu to do this
one multiplication and 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 okay 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 one even if one
single bit is one we know the number
can't be zero and then we use a final
not gate to flip this input so the
output is one only if the input number
is zero 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 four 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
didn'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 eight bits we also need a way
to specify what operation the alu should
perform for example addition or
subtraction for that we use a four-bit
operation code we'll talk more about
this in a later episode but in brief one
zero zero zero might be the command to
add while one one zero zero 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 one-bit outputs for
particular states and statuses for
example if we subtract two numbers and
the result is zero our zero testing
circuit the one we made earlier sets the
zero factor true this is useful if we
are trying to determine if two numbers
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 carryout on the add-away bill 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 of 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
crash course computer science is
produced in association with pbs digital
studios at their channel you can check
out a playlist of shows like pbs idea
channel physics girl and it's okay to be
smart this episode was filmed at the
chad and stacy emma gold studio in
indianapolis in vienna and it was made
with the help of all these nice people
and our wonderful graphics team thought
cafe thanks for watching i'll see you
later
[Music]