Tuesday, May 3, 2011

INTEGER DATA FORMATS

So far our discussion has been of whole numbers only, and even of positive whole numbers. Computers
need to keep track of the sign of a number, and must also be able to represent fractional values (real numbers).
As you might expect, if we need to keep track of the sign of a number, we can devote a bit of the computer
word to maintaining the sign of the number. The leftmost bit, also known as the most significant bit (“msb”—
in contrast to the least significant bit, “lsb,” at the right end of the word), will be zero if the number is positive,
and 1 if the number is negative. Here is a positive 6 for an 8-bit computer:
00000110
The msb is 0, so this is a positive number, and we can inspect the remaining 7 bits and see that the value is 6.
Now here’s a counter-intuitive observation. How do we represent −6? You might think it would be like this:
10000110
That would be incorrect, however. What happens if we add 1 to that representation? We get 10000111,
which would be −7, not −5! This representation does not work correctly, even in simple arithmetic computations.
Let’s take another tack. What number would represent −1? We can test our idea by adding 1 to −1. We
should get 0 as a result. How about this for negative 1:
11111111
That actually works. If we add 1 to that number, we get all zeros in the sum (and we discard the final carry In fact, the correct representation of a negative number is called the “two’s complement” of the positive
value. To compute the two’s complement of a number, simply change all the zeros to ones and all the ones to
zeros, and then add one. Here is the two’s complement of 6:
11111001 All the bits of +6 are “complemented” (reversed)
+00000001 Add one
11111010 The two’s complement of 6 = −6
You can check to see that this is correct by adding 1 to this representation 6 times. You will find that the
number becomes 0, as it should (ignoring the extra carry off the msb). You can also verify that taking the two’s
complement of −6 correctly represents +6.
Larger word sizes work the same way; there are simply more bits with which to represent the magnitude
of the number. These representations are called “integer” or “integral” number representations. They provide
a means of representing whole numbers for computation.

No comments:

Post a Comment