Doing the Math: Computers and Binary Numbers, continued
So far we have considered how computers can represent integers as unsigned quantities. It is more realistic and practical to represent them as signed integers having both positive and negative values. Let's consider how this can be done.
First, it should be observed that we will still be restricted to a uniform (finite) precision for representing signed integers. Thus, the same number of bits will now have to serve for for both negative and positive values. A reasonable approach would be to assign half of the binary values to negative decimal values and half to positive decimal values. For example, suppose that we had a precision of only four bits. Figure 2 shows how these might be assigned in an orderly fashion.
Figure 2. Dividing 4-bit precision binary into positive and negative segments.
The binary values 0-7 could represent positive decimal values and the binary values 8-15 could signify negative decimal values. This divides the range into 8 values for positives and 8 values for negatives. Notice that the binary values 0-7 representing positives have the most significant bit (MSB) of 0. The binary values 8-15 representing negatives have a MSB of 1. Thus, we could use the MSB to denote the sign and the remaining bits to signify the quantity or magnitude as shown in Figure 2. This scheme is appropriately called sign and magnitude.
Figure 3. The most significant bit denotes the sign and the last three bits signify the magnitude.
As you can see in Figure 3, each positive has a corresponding negative value that differs only in the sign bit, i.e., the MSB. With a 4-bit precision, our range is small, +7 . . . -7. But, even if we extend the precision to 16 bits or 32 bits, there are other problems that remain. First, notice that there are codes for both +0 and -0; this is not convenient if, for example, we should have to decide whether a number is 0 or not. A more serious problem arises when doing arithmetic.
Recall that any binary integer can be represented by the sequence
To do the arithmetic correctly, we have to consider the signs and the relative magnitudes of both numbers. It is much too complicated and therefore potentially expensive to implement as computer circuits.
There is another approach: using Ns-complement. Blaise Pascal, for example, employed this technique for his decimal calculating machine later known as the Pascaline.
Suppose that we had a decimal precision of 4 digits. The range of values would be from 0000 to 9999. In 9s-complement (i.e., N = 9), a negative value is determined by taking its additive inverse. This is the result of subtracting the magnitude from 9999. Here are some examples.
Simple addition operations can be used for both addition and subtraction. For example, adding a positive and negative number works provided that the carry-out value is added to adjust the sum.
Subtraction can also be solved using addition provided that the term subtracted is complemented and then added to the other term. The example illustrates.
Most computer systems employ a variation of this method for representing signed integers called twos-complement. The procedures are similar. Negative values are represented by
One advantage of this scheme is that the MSB still serves to signify the sign of the number. In general, values for which the MSB = 1 are negative; values for which the MSB = 0 are positive. Addition and subtraction can also be handled by performing simple additions.
But, in some instances, we can produce a value that it too large for our precision. This causes an overflow error condition. The results are incorrect. For instance,
In short, adding two numbers with the same sign (MSB) results in overflow when the sign (MSB) changes.
Represent the following decimal values in 2s-complement using 16-bit precision. (The binary magnitudes are given in parentheses.) You may check your answers using the signed binary converter calculator.
What are the values for each in hexadecimal notation? (Hint: 16-bit precision is expressed by a sequence of four hex digits.) Again, test your answers with the signed binary converter calculator.
This page last updated 6/05
©Abernethy and Allen, 2003