Numbers and Notations

Nonpositional Notations

Positional Notations

Binary Numbering

Converting between Decimal and Binary

Encoding Binary Numbers for Computers

Representing Binary Values in Other Bases

Negative Numbers on Computers

Twos-Complement

 

 

Doing the Math: Computers and Binary Numbers, continued

Negative Numbers on Computers

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

+4

0100

+ -3

+1011

+1

1111

= -7

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.

-11

9999

-0011

=

9988

-53

9999

-0053

=

9946

   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.

+373

0373

+ -53

+ 9946

+320

1 0319

(add carry) + 1

0320

   Subtraction can also be solved using addition provided that the term subtracted is complemented and then added to the other term. The example illustrates.

2430

2430

- 11

+ 9988

2419

1 2418

(add carry) + 1

2419

Twos Complement

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

  1. Finding the complement (bit-value inverse) of the unsigned quantity.
  2. The result is adjusted by adding +1.

Examples

decimal value
(binary) magnitude
(binary) inverse
adjusted result

-4

0100

1011

1100

+ 1

1100

decimal value
(binary) magnitude
(binary) inverse
adjusted result

-7

0111

1000

1001

+ 1

1001

   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.

Examples

+7

0111

+ -4

+1100

+3

1 0011

(ignore carry out) = +3

+3

0011

- -4

(convert to comp) +0100

+7

0111

= +7

   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,

+7

0111

- -4

(convert to comp) +0100

(too big) +11

1011

= -5

In short, adding two numbers with the same sign (MSB) results in overflow when the sign (MSB) changes.

Exercises

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.

1.

-1

(0000 0000 0000 0001)

2.

0

(0000 0000 0000 0000)

3.

-128

(0000 0000 1000 0000)

4.

-10,762

(0010101000001010)

 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
Furman University