CHAPTER 1 ■ BASIC IDEAS
12
Because a hexadecimal digit corresponds to four binary digits, you can represent any binary number in
hexadecimal simply by taking groups of four binary digits starting from the right, and writing the equivalent
hexadecimal digit for each group. Look at the following binary number:
1111 0101 1011 1001 1110 0001
Taking each group of four bits and replacing it with the corresponding hexadecimal digit from the table produces:
F 5 B 9 E 1
You have six hexadecimal digits corresponding to the six groups of four binary digits. Just to prove that it all works
out with no cheating, you can convert this number directly from hexadecimal to decimal by again using the analogy
with the meaning of a decimal number. The value of this hexadecimal number therefore works out as follows.
F5B9E1 as a decimal value is given by
15 × 165 + 5 × 164 + 11 × 163 + 9 × 162 + 14 × 161 + 1 × 160
This turns out to be
15,728,640 + 327,680 + 45,056 + 2,304 + 224 + 1
Thankfully, this adds up to the same number you got when converting the equivalent binary number to a decimal
value: 16,103,905. In C++, hexadecimal values are written with 0x or 0X as a prefix, so in code the value would be
written as 0xF5B9E1. Obviously, this means that 99 is not at all the same as 0x99.
The other very handy coincidence with hexadecimal numbers is that modern computers store integers in words
that are an even number of bytes, typically 2, 4, 8, or 16 bytes. A byte is 8 bits, which is exactly two hexadecimal digits
so any binary integer word in memory always corresponds to an exact number of hexadecimal digits.
Negative Binary Numbers
There’s another aspect to binary arithmetic that you need to understand: negative numbers. So far, I’ve assumed that
everything is positive—the optimist’s view—and so the glass is still half full. But you can’t avoid the negative side of
life—the pessimist’s perspective—that the glass is already half empty. How is a negative number represented in a
computer? Well, you have only binary digits at your disposal, so the solution has to be to use at least one of those to
indicate whether the number is negative or positive.:
For numbers that can be negative (referred to as signed numbers), you must first decide on a fixed length (in other
words, the number of binary digits) and then designate the leftmost binary digit as a sign bit. You have to fix the length
to avoid any confusion about which bit is the sign bit.
As you know, your computer’s memory consists of 8-bit bytes, so binary numbers are going to be stored in some
multiple (usually a power of 2) of 8 bits. Thus, you can have numbers with 8 bits, 16 bits, 32 bits, or whatever. As long
as you know what the length is in each case, you can find the sign bit—it’s just the leftmost bit. If the sign bit is 0, the
number is positive, and if it’s 1, the number is negative.
This seems to solve the problem. Each number consists of a sign bit that is 0 for positive values and 1 for negative
values, plus a given number of other bits that specify the absolute value of the number, the value without the sign in
other words. Changing +6 to –6 then just involves flipping the sign bit from 0 to 1. Unfortunately, this representation
carries a lot of overhead in terms of the complexity of the circuits that are needed to perform arithmetic. For this
reason, most computers take a different approach. You can get an idea of how this approach works by considering how
the computer would handle arithmetic with positive and negative values so that operations are as simple as possible.
Ideally, when two integers are added, you don’t want the computer to be messing about, checking whether
either or both of the numbers are negative. You just want to use simple “add” circuitry regardless of the signs of the
operands. The add operation will combine corresponding binary digits to produce the appropriate bit as a result, with
a carry to the next digit along where this is necessary. If you add –8 in binary to +12, you would really like to get the
answer +4 using the same circuitry that would apply if you were adding +3 and +8.