CS 100 (Learn)CS 100 (Web)Module 01


Why Do People Use Hexadecimal?

(direct YouTube link)

NOTE: If your internet access is restricted and you do not have access to YouTube, we have provided alternate video links.

TRANSCRIPT

Hexadecimal (or just "Hex" for short) is the name of the number system with base 16, and it is very popular in the world of computers, for reasons that we will see in this video.

First, let's review the first sixteen numbers, written in decimal, binary and hex:

| Decimal | Binary | Hex |
|---------|--------|-----|
|       0 |   0000 |   0 |
|       1 |   0001 |   1 |
|       2 |   0010 |   2 |
|       3 |   0011 |   3 |
|       4 |   0100 |   4 |
|       5 |   0101 |   5 |
|       6 |   0110 |   6 |
|       7 |   0111 |   7 |
|       8 |   1000 |   8 |
|       9 |   1001 |   9 |
|      10 |   1010 |   A |
|      11 |   1011 |   B |
|      12 |   1100 |   C |
|      13 |   1101 |   D |
|      14 |   1110 |   E |
|      15 |   1111 |   F |

Again, this table will be provided on exams.

As you can see from this table, each hex digit corresponds to exactly four binary digits.

To convert back and forth between binary (or base two) and hexadecimal (base sixteen) is actually quite easy.

To convert from binary to hex, you want to:

If we have the number one one one one zero one one zero zero base two [1111011002], we group the number into fours, starting on the right.

1 1110 1100

We then convert each group of four to hex, using our table.

one one zero zero becomes C, one one one zero becomes E and zero zero zero one becomes one. So the equivalent number is one E C base sixteen [1EC16].

To convert from hex to binary, you just reverse the process.

It is good to start on the right, for consistency.

If we have the number three F B in hex [3FB16]:

B becomes one zero one one, F becomes one one one one, and three becomes zero zero one one.

Reading from left to right, we see that three F B in hex [3FB16] becomes one one one one one one one zero one one in binary [11111110112].

The reason hex is so popular in the world of computers is because it is so easy to convert back and forth between binary and hex.

Hex is much more compact than binary and it makes it much more practical and less overwhelming to use.

Because each byte is exactly eight binary digits, that means each byte is always exactly two hex digits. That makes it really easy to identify how many bytes of information a number is, and what those bytes are.

It is much harder to look at a decimal number and break it into bytes. In addition, representing values in decimal sometimes hides information that is clearer in hex. The numbers forty thousand nine hundred and sixty and forty-one thousand one hundred and fifty-two [4096010, 4115210] don't look that similar in decimal, but in hex they are A zero zero zero and A zero C zero [A00016 and A0C016]. That's not very important in this course, but in in some contexts that could be quite helpful.

In practice, it's not always feasible to use subscripts to indicate that a number like one two three four is in hex [123416], and so there are two common conventions to indicate a number is in hex. Programmers often use the prefix "zero x" before the number, so it would become "zero x one two three four" [ 0x1234 ]. Web designers use the number sign or the "hashtag" prefix \so it would become hashtag one two three four [ #1234 ]. This is easy to remember because a "hashtag" looks a bit like an H.

So if you want to let someone know they are important to you, you can tell them two thousand nine hundred and ninety [2,990], which in hex becomes "hashtag" BAE.