|
A big problem with the binary system is verbosity. To represent the value 20210 requires eight binary digits. The decimal version requires only three decimal digits and, thus, represents numbers much more compactly than does the binary numbering system. This fact is not lost on the engineers who design binary computer systems. When dealing with large values, binary numbers quickly become too unwieldy. Unfortunately, the computer thinks in binary, so most of the time it is convenient to use the binary numbering system. Although we can convert between decimal and binary, the conversion is not a trivial task. The hexadecimal (base 16) numbering system solves many of the problems inherent in the binary system. Hexadecimal numbers offer the two features we're looking for: They're very compact, and it's simple to convert them to binary and vice versa. Because of this, most computer systems engineers use the hexadecimal numbering system. Because the radix (base) of a hexadecimal number is 16, each hexadecimal digit to the left of the hexadecimal point represents some value times a successive power of 16. For example, the number 123416 is equal to:
1 * 163 + 2 * 162 + 3 * 161 + 4 * 160
or
4096 + 512 + 48 + 4 = 466010
Each hexadecimal digit can represent one of 16 values between 0 and 1510. Because there are only ten decimal digits, we need to invent six additional digits to represent the values in the range 1010..1510. Rather than create new symbols for these digits, we'll use the letters A..F. The following are all examples of valid hexadecimal numbers:
123416 DEAD16 BEEF16 0AFB16 FEED16 DEAF16
Because we'll often need to enter hexadecimal numbers into the computer system, we'll need a different mechanism for representing hexadecimal numbers. After all, on most computer systems you cannot enter a subscript to denote the radix of the associated value. We'll adopt the following conventions:
All hexadecimal values begin with a "$" character, e.g., $123A4.
All binary values begin with a percent sign ("%").
Decimal numbers do not have a prefix character.
If the radix is clear from the context, this book may drop the leading "$" or "%" character.
Examples of valid hexadecimal numbers:
$1234 $DEAD $BEEF $AFB $FEED $DEAF
As you can see, hexadecimal numbers are compact and easy to read. In addition, you can easily convert between hexadecimal and binary. Consider Table 2-1.
Binary | Hexadecimal |
---|---|
| |
%0000 | $0 |
%0001 | $1 |
%0010 | $2 |
%0011 | $3 |
%0100 | $4 |
%0101 | $5 |
%0110 | $6 |
%0111 | $7 |
%1000 | $8 |
%1001 | $9 |
%1010 | $A |
%1011 | $B |
%1100 | $C |
%1101 | $D |
%1110 | $E |
%1111 | $F |
This table provides all the information you'll ever need to convert any hexadecimal number into a binary number or vice versa.
To convert a hexadecimal number into a binary number, simply substitute the corresponding 4 bits for each hexadecimal digit in the number. For example, to convert $ABCD into a binary value, simply convert each hexadecimal digit according to the preceding table:
A B C D Hexadecimal 1010 1011 1100 1101 Binary
To convert a binary number into hexadecimal format is almost as easy. The first step is to pad the binary number with zeros to make sure that there is a multiple of 4 bits in the number. For example, given the binary number 1011001010, the first step would be to add 2 bits to the left of the number so that it contains 12 bits. The converted binary value is 001011001010. The next step is to separate the binary value into groups of 4 bits, e.g., 0010_1100_1010. Finally, look up these binary values in the preceding table and substitute the appropriate hexadecimal digits, i.e., $2CA. Contrast this with the difficulty of conversion between decimal and binary or decimal and hexadecimal!
Because converting between hexadecimal and binary is an operation you will need to perform over and over again, you should take a few minutes and memorize the table. Even if you have a calculator that will do the conversion for you, you'll find manual conversion to be a lot faster and more convenient when converting between binary and hex.
|