Hex counting

Posted on Friday, December 22, 2006 in Curiosities, Science, Development

Hex counting

In the latest comic strip of Raulito el friki the hexadecimal numeration is mentioned and some readers are sorry not to dominate it. Here comes the explanation.

The decimal numeration begins with 0 and when the 9 -the last symbol- is reached, the next is 0 and we carry 1 to add it to the following more significant digit; i.e., if we add 1 plus 9, the result is 0 and we carry 1 for the following digit.

In hexadecimal, symbols don't end with 9, after this came A, B, ..., F and only after F is when we come back to 0 and add 1 to the following digit. So, the first 17 numbers in hexadecimal are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F, 10.

If for example, we have the number 39, the following would be 3A. The one that comes after 99, the 9A. After 4F comes 50. And after 9F comes A0.

To be able to add and multiply or to convert between decimal and hexadecimal the most usual is to pass through binary. In binary only to symbols are used: 1 and 0, and the value of every digit is 2position. For example, in the number 11001 the values are:


So 11001BIN = 1*24 + 1*2³ + 0*2² + 0*2¹ + 1*20 = 8 + 4 + 0 + 1 = 13DEC. We have converted from binary to decimal.
The reverse process, from decimal to binary, is also too simple, you have only to write the number as powers of 2 without repeating any, the exponents are the positions of the ones and the rest are zeros:
28DEC = 24 + 2³ + 2² = 11100BIN.

And now we will finally see how to convert from decimal to hexadecimal through binary. First of all, we have to convert the number from decimal to binary and with the resulting binary we take the bits four by four and represent their hexadecimal value. Let's see it with an example.

303DEC = 25 + 24 + 2³ + 2² + 2¹ + 20 = 100101111 BIN = 0001 0010 1111 BIN
0001BIN = 1HEX
0010BIN = 2HEX
1111BIN = FHEX
303DEC = 12FHEX

To convert from hexadecimal to binary, the reverse process is applied: we write every hexadecimal digit with its binary representation with four digits and then we convert the resulting binary number to decimal.

In the comic strip, the hexadecimal representation of the colors is mentioned. In HTML the colors are represented by a number of six hexadecimal digits, in which the first two correspond to red, the following two to green, and the last two to blue. Being FFHEX = 255DEC the maximum value they can take.

837BA5 = (Red = 83HEX, Green = 7BHEX, Blue = A5HEX) = (Red =   131DEC, Green =   123HEX, Blue =   165HEX) =