Lecture 2

Negative Numbers

In a binary number, the bit furthest to the left is called the most significant bit (msb) and the bit furthest to the right is called the least significant bit (lsb).
msb
|
10010010
       |
       lsb
Four systems for representing negative numbers:
  1. Signed Magnitude

  2. The MSB gives the sign of the number (sign bit) , 0 for positive and 1 for negative. The remaining bits hold the magnitude of the number.
    e.g.: 4=00000100, -4=10000100
     
  3. One's Complement

  4. The MSB is also the sign but to negate a number all the bits are complemented (1 is replaced by 0 and 0 is replaced by 1)
    e.g.: 4=00000100, -4=11111011

    Signed Magnitude and One's Complement are not used often because they have two representations for 0 (+0 and -0).
     

  5. Excess n Notation

  6. Add n to the number. n is usually 2m-1 where m is the precision. For an 8 bit number, n is 128.
    e.g.:4=10000100, -4=011111100 (-4+128=124)
     
  7. Two's Complement

  8. This is the same as ones complement but negative numbers have one added to them. This is so that there are not two zeros (+0 and -0). The MSB is still the sign.
    e.g.:4=00000100, -4=11111100.
Two's Complement is the most common representation for negative numbers. Because it has the following properties: It is used by the machine running the following program.

The diagram below shows the relationship between binary, signed and unsigned numbers for 8 bit two's complement.

 

Moving clockwise around this diagram corresponds to addition; moving anti-clockwise corresponds to subtraction.

Note that there are two places on the diagram where an addition or subtraction will cause a result that is not valid.

  1. For unsigned numbers, crossing the top of the diagram.

  2. 10-11=255.....Wrong.
    250+9=3..........Wrong again.
    These cases cause a Carry.
  3. For signed numbers, crossing the bottom of the diagram.

  4. 120+10=-126.....Wrong.
    -100-29=127.....Wrong again.
    These cases cause an Overflow.

Similar diagrams can be drawn for the other representations of binary numbers to illustrate their disadvantages.

In C, there are 4 standard types of integer:

int

unsigned long unsigned long char unsigned char

Addition and Subtraction of binary numbers

Two binary numbers A and B are added from right to left, creating a sum and a carry in each bit position.

Since the rightmost bits of A and B can each assume one of two values, four cases must be considered: 0 + 0, 0 + 1, 1 + 0, and 1 + 1.

For the remaining bit positions, the carry into the position can be 0 or 1, so that a total of eight input combinations must be considered.

Subtraction is performed in a similar manner using different rules. For subtraction the carry is often called a borrow.
These operations are performed by the CPU (you rarely have to perform operations on individual bits).

The order of numbers is preserved when representing signed integers using twos complement.
This means that the same addition and subtraction can be used for signed and unsigned numbers.
The only difference is that a signed arithmetic can produce overflows and unsigned arithmetic can produce carrys.