Welcome to Exploring Computing.
Today's video is Hexadecimal.
So we've seen inside the computer everything's
done with binary.
But it turns out binary is a bit unwieldy.
So in addition to the binary number of system,
and of course our own decimal number system,
computers often use the hexadecimal number system.
In fact, I would say that you're more likely to see hexadecimal
when working with computers than binary,
just because binary is just really difficult to work with.
So in this video, we're going to take
a look at how the hexadecimal number system works.
Hexadecimal is base 16.
We've previously seen that binary is base 2,
and therefore we only have two digits, 0 and 1.
And of course, decimal is base 10,
and so, we have the 10 digits, 0 through 9.
And so, hexadecimal is going to have 16 digits.
But of course, we don't have 16 digits,
so what we're going to do is we're
going to borrow the letters A, B, C, D, E, and F.
And when you add that to 0 to 9, you end up with 16 digits.
Why do we use hexadecimal?
Well, consider this sequence of binary digits here.
This particular sequence of bits is actually an add instruction
for the MIPS CPU, and treating it as binary
is somewhat of a problem.
There's a number of issues with looking at this as binary.
One, it's really long.
And as a consequence, it's pretty unwieldy.
I personally find it a little intimidating looking at these
long sequences of 0's and 1's.
But more important, it's error-prone working with all
these 0's and 1's.
It's really easy to accidentally mix one of the 0's and the 1's.
Or to just completely skip one of them
and realize when you're done that, gee, I
was supposed to have 32 bits, and I only ended up with 29.
I wonder which one I skipped.
So we want something that's a little bit more compact.
Now, we can use the decimal number system.
So if one were to work out the equivalent,
it turns out this is the equivalent of 21,710,880
in base 10.
And that is quite a bit more compact.
But moving from binary to decimal is kind of a pain.
We've seen how to do it before.
Here, we're doing it with just 4 bits.
But you can imagine doing it with the 32
bits on the previous page.
It's going to be a big mess.
And I haven't shown you how to move in the opposite direction,
from decimal to binary, because it turns out it's a real pain.
So I personally try and avoid it whenever possible.
Here's the problem, decimal is base 10.
And 10 really doesn't have anything to do with 2.
There's a number of different number systems
that work quite a bit better with binary
than the decimal number system does.
One of them is octal, and the other one is hexadecimal.
And these work much better because octal is base 8, and 2
to the third power is 8.
And hexadecimal is base 16, and 2 to the fourth power is 16.
And so, there's a natural relationship
between base 8 and 2, and base 16 and 2
that just isn't there for base 10 and 2.
And it turns out that because 16 is 2 to the fourth power,
every 4 bits corresponds to a hexadecimal digit.
So there's a natural breaking point
where you can look at every 4 bits
and say, OK, that's a hexadecimal digit, that's
a hexadecimal digit, that's a hexadecimal digit.
You can't do that at all with decimal.
And since we're not really usually working
with 4 bits at a time, we're actually
working with 8 bits at a time, it
turns out that every byte corresponds
to a pair of hexadecimal digits.
So a lot of times, when you're working with hexadecimal,
you'll see lots of pairs of hexadecimal digits.
We'll see examples of this in a minute.
OK, so here is a 24 bit color from one
of our previous lectures.
This particular color is cardinal red.
This is the official color that Stanford uses for cardinal red.
And you can see we've got our binary numbers up top.
We've got our decimal equivalents underneath it,
although converting from the binary to the decimal numbers
is, again, a bit a pain.
And so, what I'm going to show you
is how to convert from the binary to the hexadecimal.
And this is going to turn out to be easier.
All right, so we can immediately break down
each of these numbers into sets of four.
Each of these sets of four here is
going to correspond to a hexadecimal digit.
And then, to start off with, you can just use a table.
And so here's a table of all the possible combinations
of four bits and their hexadecimal equivalents.
But, as you continue to get better at it,
you'll be able to do conversion between the two pretty easily.
You can do conversions of four bits
to decimal without too much trouble,
although I do tend to stall out.
If I'm in the middle of a lecture,
and I need to do a conversion, you'll
see me puzzle over the problem for about a second or two
and just stall on lecture.
But if I need to do more than 4 bit conversion to decimal,
I'm just going to have to whip out a piece of paper.
I'm not going to do an 8 bit conversion in my head
while everybody is staring at me in lecture.
That's just-- I'm sure there are other people that
are much better at it, but I just can't do it.
All right, so we're going to take our little table here,
or we can just go with the--
the right-hand digit is our 1's, the next one is
our 2's, the next one is our 4's, and the last ones is
our 8's.
So if we follow the 1's, 2, 4, 8,
you can see that this number here, 1000, this
is actually an 8.
Or, I can look it up on my table here on the right.
OK, so 1000 base 2 is 8 base 16.
And now, let's do a full byte.
So this is a byte from our color 10001100,
and immediately, this is where it comes in handy compared
to decimal.
If I were going to do the decimal conversions,
I have to go, oh, that's the 1's, that's the 2's, that's the
4's, that's the 8's, that's the 16's, that's the 32s,
that's the 64's, so that's the 128's.
And if the number gets much bigger,
you know, 256, 512, 1024, and trying
to remember which place goes with what gets really messy.
But hexadecimal, I don't do that.
I just split this thing in half.
I say, I just want sets of 4 bits.
So I say I've got two sets of 4 bits, one set on the left, one
set on the right.
The set on the left we already did,
but you can do it really using in your head, 1248.
Remember, each of the places is 1's, the 2's, the 4's, and the
8's.
And so we could see we're going to have 8 on the left.
On the right, we've got 1248.
And that's 12 in decimal.
And again, that seems pretty easy.
You're like, why don't we just use decimal?
The problem is that the decimal conversion only
works quickly if we have small numbers of bits.
The hexadecimal conversion always
works quickly because we never have more than four bits
because we can always break the binary number into sets
of four bits.
All right, so we can look this up in our table,
or we can do it in our head, and we
can see that this corresponds to a C.
And so, we can see that this particular bit sequence,
we've converted the first four bits to 8
and the second four bits to C, and so, combined together,
we can see that this whole sequence is 8C.
Let's do another one.
And so, this is another one of the components
of our cardinal red color.
And I take the bits, and I immediately
break them into sets of four.
And I can see I have two sets of four.
The set on the left is super, super easy
because that's clearly a 1.
And the set on the right, we have
our 1's, our 2's, and our 4's and so I can see that's a 5.
And so, I can see that this converts to 15.
And in fact, here's the full-color sequence here.
The amount of red is 8C, the amount of green is 15.
And the amount of blue is 15.
And this is actually how it's commonly
going to be represented.
And so, what we see here is the official color
guide from Stanford.
And you can see they've got a bunch
of different possible representations of this,
including-- remember, we've got the RGB.
That's our additive color, red, green, blue.
And then, we have our subtractive color for printing.
That's our CMYK-- cyan, magenta, yellow, and key or black.
And you can see down at the bottom, they have the hex.
And they have the hex listing, which is what we originally
converted this to.
They have that hex listing there because that's
what the world wide web uses.
And then, this is the color selector in Photoshop.
Photoshop has a web color setting.
And you can see that the web color setting, again,
uses these hexadecimal digits.
So the point is, if you're going to be doing art, particularly
art for the web, you should be familiar
with hexadecimal numbers.
These hexadecimal numbers are going
to show up in lots of other places as well.
When we're looking at addresses on the network,
we'll see that hexadecimal digits show up
on network physical addresses.
These are the addresses associated with your device
that you have to tell Stanford when you go ahead
and register your computer with Stanford,
so it will let you on the network.
Hexadecimal numbers are commonly used
when identifying individual bits in computer memory.
And another place you may see hexadecimal numbers
is when you see error messages.
When the computer crashes, you often see hexadecimal,
not super helpful, but you know you'll recognize and be like,
oh, that's great, my computer crashed on me.
Oh, and hey, look, a hexadecimal number,
I learned that in my computer science class.
You could tell you're looking at hexadecimal when you're
looking at something that kind of looks like a number,
but in addition to the digits 0 through 9,
you have A, B, C, D, E, and F mixed in.
So you can see on the bottom right there,
I have what looks kind of like a bunch of numbers,
but I also have those F's and A's in there.
And so, once you get trained for this,
you'll immediately look at that and say, oh,
that must be hexadecimal.
In addition, though, it turns out
that there's a convention that is sometimes used, not always.
But, when we're working with computers,
often if a number is preceded by a single 0,
that indicates that we're actually
looking at a octal number.
And octal is used because octal is base 8,
and 8 is a power of 2.
And so, I think it was used quite a bit for a while.
But we mostly moved to hexadecimal.
But if you see something with a 0 followed
by a bunch of digits, that's an octal number.
And if you see something starting with a 0x,
as we see in our computer memory here,
that's representing a hexadecimal number.
So you look at this little sequence here.
If we just see a 12 by itself, that's probably base 10.
If we see 012, that's a 12 in base 8.
And if we see a 0x12, that's a 12 in base 16.
All right, that's it for our little introduction
to hexadecimal. but you will be seeing hexadecimal
throughout the quarter.