Monday 28 November 2011

What is hexadecimal? in C programming

 What is hexadecimal?

Hexadecimal is the base 16 numbering system. It is the most commonly used numbering system in  computers. In hexadecimal, or hex for short, you count 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 1A, 1B, 1C, 1D, 1E, 1F, and so on. Don’t get too bothered by the letters— they are merely single-digit placeholders that represent (in decimal) 10 through 15. Remember the rule of counting in different bases—count from 0 to the number that is one less than the base. In this case, it is 15. Because our Western numbers do not contain single-digit values representing numbers above 9, we use A, B, C, D, E, and F to represent 10 to 15. After reaching 15, or F, you can move to two decimal, rather hexadecimal, places—in other words, 10. As with octal and binary, compare hex counting to decimal counting (once again, decimal is on top):

1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, ...
1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F, 10, ...

Note that “10”, what we historically have called ten, in decimal is equal to “A” in hex. As with the previously discussed numbering systems, when you increase to two or three or more hexadecimal places, you are increasing by a power of 16. 1 is 160, 16 is 161, 256 is 162, 4096 is 163, and so on. Therefore, a he number such as 3F can be converted to decimal by taking (3 ´ 161) + (F ´ 160), which equals (48 + 15), or 63 in decimal (remember, F is 15 in decimal). A number such as 13F is (1 ´ 162) + (3 ´ 161) + (F ´ 160), which equals (256 + 48 + 15), or 319 in decimal. In C source code, you will see these numbers shown as 0x3F or 0x13F. The “0x” is used to show the compiler (and the programmer) that the number being referenced should be treated as a hexadecimal number. Otherwise, how could you tell whether the value “16” were decimal or hex (or even octal, for that matter)? A slight modification can be made to Table XX.22 by adding hex counting to that table (see Table XX.24).

Binary, decimal, hexadecimal conversion table.
------------------------------------------------------------------------------------------------------------
Binary                                     Decimal Value                   Binary Power                  Hex           Hex Power
-------------------------------------------------------------------------------------------------------------
0000                                       0                                                                             0
0001                                       1                                       2power of 0                   1              16power of 0
0010                                       2                                       2power of 0                   2
0011                                       3                                                                             3
0100                                       4                                        2power of 0                  4
0101                                       5                                                                             5
0110                                       6                                                                             6
0111                                       7                                                                             7
1000                                       8                                       2power of 0                   8
1001                                       9                                                                             9
1010                                       10                                                                           A
1011                                       11                                                                           B
1100                                       12                                                                           C
1101                                       13                                                                           D
1110                                       14                                                                            E
1111                                       15                                                                            F
1 0000                                    16                                     2power of 0                   10             16power of1

 I added one more count at the bottom, taking the total to decimal 16. By comparing binary to decimal to hex, you can see that, for example, ten is “1010” in binary, “10” in decimal, and “A” in hex. In the last line, sixteen is “1 0000” or “10000” in binary, “16” in decimal, and “10” in hex. What does all this mean? Because today’s 16-, 32-, and 64-bit processors all have bit widths that happen to be multiples of 16, hexadecimal fits very nicely as the numbering system for those types of computers. Another added quality is that hex is also a multiple of binary. Note that the last line of Table XX.24 shows the binary value broken into two sections (1 0000). Four binary digits (or four bits) can count up to 15 (16 unique numbers when you include zero). Four bits also equals a nibble. By looking at the table, you can see that one digit of hex can also count up to 15 (16 unique numbers because we include zero). Therefore, one hex digit can represent four binary digits. A good example is (decimal) 15 and 16. Fifteen is shown as 1111 in binary, the highest number four binary digits can count. It is also shown as F, the highest number one hex digit can count. After you go to 16, it takes five binary digits (1 0000) and two hex digits (10). The following lines convert the same numbers used earlier (0x3F and 0x13F) to binary:

3F 111111
13F 100111111

If you replace the leading spaces with zeros and break the binary digits into groups of four, it might become
a little clearer:

03F 0 0011 1111
13F 1 0011 1111

You are not required to break binary numbers into groups of four—it’s simply easier to count when you know that every four binary digits equal one hex digit. To prove that the numbers are equal, I’ll convert the binary representations to decimal (because the hex values were already converted in a previous paragraph). 111111 would be (1 ´ 25) + (1 ´ 24) + (1 ´ 23) + (1 ´ 22) + (1 ´ 21) + (1 ´ 20), which equals (32 + 16 + 8 + 4 + 2 + 1), or 63 in decimal, just as before. The number 1 0011 1111 would be + (1 ´ 28) + (0 ´ 27) + (0 ´ 26) + (1 ´ 25) + (1 ´ 24) + (1 ´ 23) + (1 ´ 22) + (1 ´ 21) + (1 ´ 20), which equals (256 + 32 + 16 + 8 + 4 + 2 + 1), or 319 in decimal. Hexadecimal and binary fit together like hand and glove.

Cross Reference:

XX.22: What is binary?
XX.23: What is octal?

No comments:

Post a Comment