Sams Teach Yourself C++ in 21 Days

(singke) #1
1*16 = 16. 28-16 = 12
1*8 = 8. 12-8 = 4
1*4 = 4. 4-4 = 0
0*2 = 0
0*1 = 0
124+60+28+12+4 = 252.
Thus, the answer in binary is 11111100.
Now, it turns out that if you treat this binary number as two sets of four digits (1111
1100), you can do a magical transformation.
The right set is 1100. In decimal that is 12, or in hexadecimal it is C. (1*8 + 1*4 + 0*2 +
0*1)
The left set is 1111, which in base 10 is 15, or in hex is F.
Thus, you have:
1111 1100
F C
Putting the two hex numbers together is FC, which is the real value of 1111 1100. This
shortcut always works! You can take any binary number of any length, and reduce it to
sets of four, translate each set of four to hex, and put the hex numbers together to get the
result in hex. Here’s a much larger number:
1011 0001 1101 0111
To check this assumption, first convert this number to decimal.
You can find the value of the columns by doubling. The rightmost column is 1, the next
is 2, then 4, 8, 16, and so forth.
Start with the rightmost column, which is worth 1 in decimal. You have a 1 there so that
column is worth 1. The next column to the left is 2. Again, you have a 1 in that column,
so add 2 and for a total of 3.
The next column to the left is worth 4 (you double for each column). Thus, you have
4+2+1 = 7.
Continue this for each column:

814 Appendix A

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

Free download pdf