(A Hexadecimal And Binary Primer)
By: David Ray
A long time ago, somebody decided that humans should use a base 10 number system. It was probably decided upon because of the fact that we have 10 fingers. Since most people didn’t wear shoes back then I think we should just be thankful that we don’t use base 20. Now millenniums later these new machines come around called computers, and unfortunately, they don’t seem to have 10 fingers. Basically, they have one finger to use for counting. With a single finger you can only represent 2 numbers, zero and one. This is called a binary system. So how do computers count beyond one and what does that have to do with hexadecimal? Well that’s what this article is all about.
First we’re going to cover base ten. I know that most of you already know how to count in base ten, but it helps to understand the reasons when you’re trying to understand other number bases. Basically the way it works is that you count 0 (zero) to 9 and then add another digit. And you do that for every column. Contrary to popular belief you always start at zero, not one. When you fill up the ones column, and you’ve counted to 9, the next number is a 0 in the ones column, and a 1 in the tens column. There was already an implied 0 in the tens column, and you increment that to the next digit which is 1. So you have one zero (10), which is known as ten. Then you repeat that process until you have a 9 in the tens column and a 9 in the ones column. When you go to the next number you replace the 9 in the ones column with a 0, and replace the 9 in the tens column with a 0, and you replace the implied 0 in the hundreds column with the next digit which is 1. You now have one zero zero (100), also known as one hundred. So the rule is that you count from zero to the base minus one (in this case 10 minus 1 which is 9), and then you add another column.
Counting in other number bases works exactly the same way. Since computers work in base two, or binary, we’re going to start with that one. So if you want to count on a computer, you start with 0, and you go to 1. Now you have the base minus one in the ones column, and that’s the largest value you can have there. So for the next number, replace the 1 in the ones column with a 0, and put a 1 in the twos column. It’s called the twos column because we’re in base two, just like the tens column in base ten. So the number you have is one zero (10), which is the equivalent of 2 in base ten. The next number is simple, just increase the ones column to a one, and you have a one one (11). Which is 3 in base ten. Now you have the maximum value in the ones and twos columns, so you replace the implied 0 in the fours column with a 1 and replace the 1’s in the ones and twos columns with zero. You now have one zero zero (100) which is equivalent to a four in base ten.
The third columns is the fours column in binary (base two) for exactly the same reason that the third column in decimal (base ten) is the hundreds column. In all number bases the first column is the ones column, all subsequent columns are the previous column multiplied by the number base. So in decimal, the first column is the ones column, and the next column is the tens column because it’s one multiplied by ten. The next column would be the hundreds column because it’s ten multiplied by ten. In binary, the first column is the ones column (just like all other number bases), and the next column is the twos column, because one multiplied by two is two. The next column is the fours column, because two multiplied by two is four. And the next column is the eight’s column, four multiplied by two. And so on to infinity.
Most modern computers, and certainly all personal computers work in binary or base two. This is because they’re designed as a sequence of on and off switches. And with a single on and off switch you can only represent two values, and therefore base two. While the computers can handle any number in base two, regardless of how large it is, humans tend to have problems after about 4 digits or so. So a more efficient way of communicating large values with computers was necessary. So most programmers use hexadecimal, also known as base sixteen, numbers.
Hexadecimal works exactly the same way as other numbers bases, where you count from 0 to the base minus one. Since the base minus one is greater than nine however, you need to have more digits. So we use the alphabetic characters "A" through "F" to represent the six digits above nine. In hexadecimal we count: 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E, and F. Once we reach F in the ones place, we replace the implied zero in the sixteen’s place with a 1, and put a 0 in the ones place, giving us one zero (10). We then count up to "F" in both columns until we have FF, and then we replace the implied zero in the two-hundred-fifty-sixes column. We calculate the column values by taking the previous column multiplied by the number base, so our first column is ones, our second column is sixteen’s, and our third column is two-hundred-fifty-sixes. That’s sixteen multiplied by sixteen. The fourth column would be the four-thousand-ninety-sixes column.
In the C programming language hexadecimal number are written with a leading 0x and then the value. This standard has been adopted by many people, and I will use it for the remainder of this document, so one zero in hexadecimal is written as 0x10. For binary values I will add a "b" at the end, this is an older standard that is rarely used today, but as far as I know there is not widely accepted standard for writing binary values.
The reason we use hexadecimal instead of decimal is because there is
an exact four to one conversion from binary to hexadecimal. Where
as with decimal, or base ten, the conversion is somewhere between three
and four digits. The decimal number of 10 is the same as the binary
value of "1010b". While the hexadecimal value for 0x10 is the same
as the binary value "10000b". The same is true of the hexadecimal
value 0x100, it’s binary counterpart is "1 0000 0000b" (spaces added for
legibility). Whereas the decimal value of 100 is "1100100b" in binary.
This makes it so that you only have to memorize sixteen conversions from
binary to hexadecimal, and you can very quickly convert any size number
between those two number bases. Here’s a chart counting to 16 in
all three number bases discussed here:
As a fairly quick way of converting from any number base to base ten you can use exponents. If you start counting the columns from the right as column zero, you can take the value in the column multiplied by the base raised to the power of the column number and add them all together. So you can take a hexadecimal number like 0x4321 and turn it into a decimal number by doing the following (using the "^" symbol to represent exponents):
1 x 16 ^ 0 = 1 (zero to the sixteenth power because the first column
is column number zero)
I should point out that any number raised to the power of zero is equal to one. This is a standard math rule, and is not special math because of computers. Using these rules you should be able to convert to and from any number base, not just the 3 mentioned in this article. In the old days it was quite common to use Octal, or base eight, number as well.
Last revised: March 24, 1999