Introduction to Hexadecimal
Composed of "hex" meaning six(6), and "dec" meaning ten(10), the word hexadecimal means six and ten, or sixteen. The hexadecimal, or base 16 number system is important to programmers because it's a shorthand way of writing out and inputting binary. It's simpler and easier because you can work with fewer symbols, or "numbers".
Please note, this article builds upon the knowledge gained in Introduction to the Binary, or Base 2, Number System
. If you're not familiar with binary, please read that article first and then continue reading below.
The hexadecimal number system is based on 16 symbols. In other words, there are sixteen(16) symbols to count with. The symbols are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 and A, B, C, D, E, and F. The use of upper case letters is a bit of a switch but don't panic. As you might suspect A does equal 10; B, 11; C, 12; D, 13; E, 14, and F, 15.
A-F are used to keep our counting numbers all in single digits. If we represented 11 as 11, for example, and not as B, we would have trouble knowing what placeholder column each of the 1s belonged in. Here is an example.
1B5 in hexadecimal numbering would be rewritten as 1115. But what does that mean. We have a 5 in the ones column but is that a 1 or an 11 in the next column, and so forth. The use of A-F removes this confusion.
In hexadecimal, just like in the binary and base 10 number systems, we start numbering, or counting, in the first, or far right column, which represents the smallest unit, ones. Then, in hexadecimal, as each new column is added, its value is 16 times larger than the previous column.
With the hexadecimal number system we start out with a 1s ("ones") column on the far right, then working from right to left, we move to a 16s column; a 256s column; a 4096s column; a 65,536s column; and so forth. Each column is a placeholder of "groups" that are 16 times larger than the groups in the column to its right.
Here are the first five placeholder columns in hexadecimal, though we could expand them out even further as the "and so forth" below anticpates:
- ....and so forth....65,356 4096 256 16 1
Every hexadecimal number will be represented by symbols, each of which is positioned in one of the unique placeholder columns above.
Notice that "16" is 16 times larger than 1; "256" is sixteen times larger than 16; 4096 is 16 times larger than 256; and 65,536 is 16 times larger than 4096.
We can also represent the value of the hexadecimal placeholders the long way:
1 = 1s column
16×1 = 16s column
16×16 = 256s column
16×16×16 = 4096s column
16×16×16×16 = 65,536s column
Or, we can represent the value of each placeholder column in exponential notation:
160 (16 to the power of 0) = 1
161 (16 to the power of 1) = 16
162 (16 to the power of 2) = 256
163 (16 to the power of 3) = 4096
164 (16 to the power of 4) = 65,536
Now, we could go further but we won't because we don't need to go further. As you can see, hexadecimal gets us into pretty large numbers (65,536) very quickly. And that is its beauty and its significance. Hexadecimal can tell a computer a whole lot in very few symbols. We might say it is packed with information which we'll demonstrate in a moment.
Counting in Hexadecimal
Alright, let's start counting and representing numbers in hexadecimal. Now remember, we have 16 symbols (numbers) to count with, 0-9 and A-F.
Let's say we just start counting:
Remember, A = 10; B = 11; C = 12; D = 13; E = 14; and F = 15
Oops! We're out of numbers. Now, what do we do? Well, let's name our next number "16". Then, let's create another column to count with and call it the "16s" column, actually a "groups of 16 column". Then, let's go on counting.
The thing to note in the above graphic is that we're multiplying each hex symbol by its respective column or placeholder value. For example, to represent the number "26" in hex we take the hex symbol "A" which equals 10 and multiply it by the column value which is 1. We do the same for the column to the left multiplying 1 by its placeholder value of 16. Thus, we have 10 + 16 = 26.
If we kept on counting, we could count to 255 with just a 16s and a 1s column before we would run out of numbers and have to start our 256s column. Let's skip ahead in our counting and do just that- run out of numbers at 255 and have to start a 256s column. Here are the last 10 numbers counting up to 255 and the first 10 numbers after we create a 256s column:
Note particularly, that whenever the number in any column reaches 10, you substitute an "A"; if it reaches 11, a "B"; 12, a "C"; 13, a "D"; 14, an "E"; and 15, an "F".
Hexadecimal is packed with information
Now, let's go in reverse. Let's start with a hexadecimal number (a pretty big one) and convert it into decimal. Then, once we've done that, we'll go even further and convert both into their binary equivalent. Here's how you do it. The graphic below should help.
We have the hexadecimal number C9A1.
C is in the 4096s column so we have to multiply C, "recognizing" it as 12, times 4096.
12 x 4096 = 49,152.
We have 9 in the 256s column so we have to multiply 9 x 256 = 2304.
We have A, which is 10, in the 16s column, so we have to multiply 10 x 16 = 160.
Finally, we have 1 in the 1s column so we have to multiply 1 x 1 = 1.
We then add these 4 hex column values together and we get 51, 617.
C9A1 in hex = 51,617 in decimal.
But how do we convert C9A1 to binary code. Well, in the binary row on our graphic above, we have to reproduce each hexadecimal column's "computed" value in the 4 bits of specific binary code placeholders directly below it.
Immediately below C, in the binary row, we have to use binary's "0s" or "1s" to get 49,152 out of the column placeholders in that binary row. The answer is 1100. We placed a 1 in the 32,768 column and a 1 in the 16, 384 column.
1 x 32,768 + 1 x 16, 384 = 49,152 just like in the hex row just above it.
Then, we go on to the remaining 3 hex columns and perform the same operation on them. Go ahead and do this yourself.
Once you have computed the values of the remaining three hex columns, add the values together. You will have:
49, 152 + 2304 + 160 + 1 = 51,617
You have successfully converted the hexadecimal number C9A1 into its decimal equivalent 51,617. And you have converted C9A1 into its binary equivalent 1100100110100001.
Now, let's continue with the Practice Problems