|
by
kirupa | 28 November 2009
In college, one of the classes I took was called
Computation Structures. In that class, you learn
how applications work, how memory is handled, what
instructions a processor needs to do something, what
pipelining does behind the scenes, etc. You even
write code (and lots of it!) in languages that
resemble assembly and machine code. I took this
class years ago, so most of it is fuzzy at this
point (most of it was fuzzy when I took the class
also!), but the one thing I still remember is how to
count in hexadecimal.
Knowing how to count in hex (what the cool kids
call hexadecimal) is not as important for many types
of code you may end up writing today. Chances are,
you may never consciously run into it in your
projects. Despite its seeming rarity in your world,
there is some value to learning how all of this
works.
So the best way I can explain how to count in hex is
by looking at how we count “normal” numbers which
are categorized as base-10. In base-10, we have our
10 numbers: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9. To
represent a number like 314, you do the following:
Notice that you subdivide your number by digits.
The 1’s digit is actually 10^0, the 10’s digit is
actually 10^1, and so on. So a number like 314 is
actually the sum of:
It is the above process that you use to represent
almost all of the numbers you see every day in the
real world. Much of what goes on inside your
computer, though, is represented in powers of 2. The
most common ones are binary and hexadecimal which
are base-2 and base-16 respectively.
The above table approach is great for taking an
existing number and breaking it down by 10’s. For
non-base 10 pairs, unless you are really good at
subdividing by whatever base you are in, it is very
difficult to use the above method and go from a
decimal number to something like a
hexadecimal/base-16 number. After all, where do you
draw the cut-off line for something like 314 when
dealing with powers of 16? Let’s revisit our base-10
numbers and try a different approach.
Let’s do a division based method instead where
you keep dividing your number by your base, in this
case 10, until you get an answer of zero. It is the
remainders that are helpful as you will see:
Now, you just put your numbers back together in
backwards order: 3.1.4 or 314. This approach
scales well across all bases (which
are belong to us), so I will use this method for
explaining how to convert normal numbers into
hexadecimal values.
When it comes to hex, your numbering is a little
odd. You start off as usual by counting up from 0,
1, 2, 3, 4, 5, 6, 7, 8, and 9. Once you get to 10,
though, you switch to letters. 10 would be
A, 11 would be B, 12 is C, 13 is D, 14, is E, and 15
is F. So your numbering goes from 0-9 and A-F to
represent 0 through 15 in our decimal world.
It does get easier from there though! Let’s look
at how to convert the decimal number 314 to hex
using the division method:
The main thing to notice is that I am dividing by
my base number, which in this case is 16. So, like
before, reading our numbers backwards, we get 1.3.10
or 13A because decimal 10 is A in hex numbering. If
you want to go from hex to decimal, just remember
that you are in base-16 and use the earlier table
approach:
You can now sum up the individual values:
That's all there is to counting in hex.
Hopefully this gives you a good understanding of how
to think about representing decimal and hexadecimal
numbers. I tried to make the explanation abstract
enough where you can figure out what the values
would be for a number of any base! Word on the
street is that famous programmer
Charles Simonyi prefers base -2 when
interviewing candidates.
I hope the information helped.
If you have any questions or comments, please don't hesitate to post them on the
kirupa.com Forums. Just post your question and I, or our friendly
forum helpers, will help answer it.
The following is a list of related tutorial and help resources that you may find
useful:
|