Wednesday, July 22, 2009

Hexadecimal

Not everyone sees the point with the hexadecimal numeral system, often called hex for short. It might be seen as just another weird system that isn't decimal.

I like hexadecimal since it has a direct correlation to the binary numeral system.

Each number in the hexadecimal system represents four numbers in the binary system. This is because hexadecimal uses the radix 16, and binary the radix 2. And 2^4 = 16.

The usefulness of hex quickly becomes apparent when you are working with large binary numbers. If you want to translate 0101 to decimal, you have to find the value for each of the numbers individually and add them up.

In this case it isn't very hard. 8*0 + 4*1 + 2*0 + 1*1 = 5. And in hexadecimal 5 is still 5.

Lets try a longer binary number, 1001 0101. Now it gets harder (more time consuming) to convert it to decimal.

128*1 + 64*0 + 32*0 + 16*1 + 8*0 + 4*1 + 2*0 + 1*1 = 149

This is when hexadecimal starts to shine. Remember that one hexadecimal number equals four binary numbers? That means that we can separate the binary numbers and translate them to hexadecimal in groups of four.

First we translate 0101, since we already know that's 5 in decimal and hex.

Then we translate 1001 to decimal.
8*1 + 4*0 + 2*0 + 1*1 = 9. So in hexadecimal 1001 0101 is written 95.

Binary          1001 0101
Hexadecimal  9      5

This means that we never have to work with larger binary numbers then four bytes when translating to hex. Since the range of four bits in decimal is 0 to 15, this is something that comes naturally after a short time of working with them.

Does this mean that hex is easy to read? Of course not, but it's easier to read then binary, and since there is a direct correlation between hex and binary, that's what makes hex useful.

No comments: