previous section | next section

Week 7: Data Storage

Section 2: Hexadecimal Numbers

When a computer prints out a value stored in its memory for the programmer to see (for example in CodeWarrior you can have the debugger do this for you) it usually does not print out a sequence of 1s and 0s. That would be too difficult for the programmer to read. On the other hand, it doesn't usually print out the contents in decimal, because this removes the programmer too far from the reality of what is being stored in the computer. It typically uses hexadecimal (base 16) numbers to do this. Base 16 numbers are used because they are relatively easy for a programmer to read due to their conciseness (every four binary digit is equivalent to one hexadecimal digit), and (more importantly) it is extremely easy to convert back and forth between binary numbers and hexadecimal numbers.

Just like each digit in a base 10 number can represent 10 different values, each digit in a base 16 number can represent 16 different values. Because we are used to using only 10 unique digits (0 through 9), we have to create 6 more digits for use with hexadecimal numbers. The convention is to use the letters A through F. So A is the digit with value 10, B is the digit with value 11, C is the digit with value 12, and so on to F which has a value of 15.

When we convert from binary to decimal we have to treat the entire binary number as a unit (so if we had a binary number with 32 digits, we would have to calculate, among other things, 231, 230, 229, and so on). When we convert binary to hexadecimal, however, we can break the binary number up into chunks of four digits (add zeroes to the left end of the number if necessary) and convert each one independently. This is because of the fact each hexadecimal digit can represent exactly the same values as 4 binary digits.

As an example, let's convert the binary number 1011110000011101 into hexadecimal. First we break it up into chunks of four digits:

1011

1100

0001

1101

Now we can convert each of these 4 digit chunks into a hexadecimal digit. The best way to do this is to convert from binary to decimal (something we already know how to do) and then decimal to hexadecimal. Let's start from the right. Recall that the place value of each of the four digits is 8, 4, 2, and 1. So 1101 == 1 X 8 + 1 X 4 + 0 X 2 + 1 X 1 == 13. Converting this decimal number to a hexadecimal digit gives D (remember, A == 10, B == 11, and so on). So our rightmost hexadecimal digit will be D. Moving left, the next binary number is 0001. 0001 == 0 X 8 + 0 X 4 + 0 X 2 + 1 X 1 == 1. So the second hexadecimal digit (counting from the right) is 1. Moving left, the next binary number is 1100. 1100 == 1 X 8 + 1 X 4 + 0 X 2 + 1 X 1 == 12. So the third hexadecimal digit (counting from the right) is C. Finally, the last binary number is 1011. 1011 == 1 X 8 + 0 X 4 + 1 X 2 + 1 X 1 ==11. So the last hexadecimal digit is B. Conclusion: the binary number 1011110000011101 is equivalent to the hexadecimal number BC1D.

This may seem a bit tedious, but once you have done a few of these it becomes very easy to see in your mind's eye a hexadecimal digit for each four digit pattern of 1's and 0's.

To convert from hexadecimal back to binary, do the same thing in reverse: for each hexadecimal digit, convert it to decimal and then find the four digit binary number that is equivalent to that decimal number. For example, to convert the hexadecimal number D7 into binary, first realize that D represents the decimal value 13, which in binary is 1101. Next realize that 7 in hexadecimal is the same as 7 in decimal. Converting to binary, 7 is 0111. So D7 converted to binary would be 1101-0111 (you wouldn't normally put the dash in there. That's for clarity only).

Notice that in the textbook a table is given which shows each hexadecimal digit and the equivalent binary number. Apparently, the preferred method is to look up each digit in this table. I prefer to know how to do the conversion myself, so I am not dependant on a table.

previous section | next section

Created by David Harden.
Please do not print or copy except for a single copy
for students taking CIS10A at Santa Rosa Junior College