' + pPage + ''; zhtm += ''; window.popUpWin.document.write(zhtm); window.popUpWin.document.close(); // Johnny Jackson 4/28/98 } //>
Teach Yourself C in 21 Days

 Previous Chapter  Next Chapter  Contents 
As a computer programmer, you might sometimes be required to work with numbers expressed in binary and hexadecimal notation. This appendix explains what these systems are and how they work. To help you understand, let's first review the common decimal number system.
The decimal system is the base10 system that you use every day. A number in this systemfor example, 342is expressed as powers of 10. The first digit (counting from the right) gives 10 to the 0 power, the second digit gives 10 to the 1 power, and so on. Any number to the 0 power equals 1, and any number to the 1 power equals itself. Thus, continuing with the example of 342, you have:
3  3 * 10^{2} = 3 * 100 = 300 
4  4 * 10^{1} = 4 * 10 = 40 
2  2 * 10^{0} = 2 * 1 = 2 
Sum = 342 
The base10 system requires 10 different digits, 0 through 9. The following rules apply to base 10 and to any other base number system:
Now let's look at the other number systems.
The binary number system is base 2 and therefore requires only two digits, 0 and 1. The binary system is useful for computer programmers, because it can be used to represent the digital on/off method in which computer chips and memory work. Here's an example of a binary number and its representation in the decimal notation you're more familiar with, writing 1011 vertically:
1  1 * 2^{3} = 1 * 8 = 8 
0  0 * 2^{2} = 0 * 4 = 0 
1  1 * 2^{1} = 1 * 2 = 2 
1  1 * 2^{0} = 1 * 1 = 1 
Sum = 11 (decimal) 
Binary has one shortcoming: It's cumbersome for representing large numbers.
The hexadecimal system is base 16. Therefore, it requires 16 digits. The digits 0 through 9 are used, along with the letters A through F, which represent the decimal values 10 through 15. Here is an example of a hexadecimal number and its decimal equivalent:
2  2 * 16^{2} = 2 * 256 = 512 
D  13 * 16^{1} = 13 * 16 = 208 
A  10 * 16^{0} = 10 * 1 = 10 
Sum = 730 (decimal) 
The hexadecimal system (often called the hex system) is useful in computer work because it's based on powers of 2. Each digit in the hex system is equivalent to a fourdigit binary number, and each twodigit hex number is equivalent to an eightdigit binary number. Table C.1 shows some hex/decimal/binary equivalents.
Hexadecimal Digit  Decimal Equivalent  Binary Equivalent 
0  0  0000 
1  1  0001 
2  2  0010 
3  3  0011 
4  4  0100 
5  5  0101 
6  6  0110 
7  7  0111 
8  8  1000 
9  9  1001 
A  10  1010 
B  11  1011 
C  12  1100 
D  13  1101 
E  14  1110 
F  15  1111 
10  16  10000 
F0  240  11110000 
FF  255  11111111 
© Copyright, Macmillan Computer Publishing. All rights reserved.