CS 111: Introduction to Computer Science

Notes on hexadecimal numbers

Since I neglected to discuss hexadecimal numbers in class today, here's a brief description.

The base-16 or hexadecimal system is like base 2 or base 10 or base 7 or whatever. You have numbers expressed with a certain set of symbols, using a place system like the ones/tens/hundreds/thousands... places of base ten numbers. The questions for hexadecimal numbers are: what symbols do we use (since there are only ten digits) and why should we bother?

In answer to the first question, we use the following symbols to represent the numbers zero through fifteen: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F. Thus, the hexadecimal number D6F (often denoted 0xD6F, where the "0x" means "hexadecimal") is:

D*16^2 + 6*16^1 + F*16^0 = 13*256 + 6*16 + 15 = 3439

As to the second question (why bother with base 16?), it's because there is a very nice relationship between binary numbers and hexadecimal numbers that enables us to represent binary numbers very compactly in hexadecimal. First, consider this chart:

Decimal Binary Hexadecimal 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

Now, suppose you have a big binary number (100100001011101010) that you want to remember. Do this:

Start with the number: 100100001011101010 Split it into four-bit chunks: 0010 0100 0010 1110 1010 Write the chunks as hexadecimal digits: 242EA

That's your 20-bit binary number, in just five hexadecimal digits. Easy to remember, store, write down, etc. Since a lot of quantities in the computer world are most naturally expressed as binary numbers, using hexadecimal numbers instead is very common. It's a snap to go between hexadecimal and binary and back, but not so easy when base ten becomes involved.

If you do anything with color charts on-line (Google "color chart" to see some), you'll find that colors are almost always described as six-digit hexadecimal numbers. For example, 0x3366FF is a light blue. It has 0x33 = binary 00110011 = decimal 51 for its red component, 0x66 = binary 01100110 = decimal 102 for its green, and 0xFF = binary 11111111 = decimal 255 for its blue.