Sams Teach Yourself C++ in 21 Days

(singke) #1
Working with Numbers: Binary and Hexadecimal 813

A


Hexadecimal ........................................................................................................


Because binary numbers are difficult to read, a simpler way to represent the same values
is sought. Translating from binary to base 10 involves a fair bit of manipulation of num-
bers; but it turns out that translating from base 2 to base 16 is very simple, because there
is a very good shortcut.
To understand this, you must first understand base 16, which is known as hexadecimal.
In base 16, there are sixteen numerals: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, and F.
The last six are arbitrary; the letters A–F were chosen because they are easy to represent
on a keyboard. The columns in hexadecimal are
Column 4321
Power 163 162 161 160
Value 4096 256 16 1

To translate from hexadecimal to decimal, you can multiply. Thus, the number F8C
represents:
F * 256 = 15 * 256 = 3840
8 * 16 = 128
C * 1 = 12 * 1 = 12
3980
(Remember that F in Hexadecimal is equal to 15 10 .)
Translating the number FC to binary is best done by translating first to base 10, and then
to binary:
F * 16 = 15 * 16 = 240
C * 1 = 12 * 1 = 12
252
Converting 252 10 to binary requires the chart:
Column 987654321
Power 28 27 26 25 24 23 22 21 20
Value 2561286432168421

There are no 256s.
1*128 = 128. 252-128 = 124
1*64 = 64. 124-64 = 60
1*32 = 32. 60-32 = 28

29 0672327112_app_a.qxd 11/19/04 12:30 PM Page 813

Free download pdf