# 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:
• Positive numbers are the same as the unsigned representation.
• Unsigned addition and subtraction works for signed numbers.
• There is only one representation for zero.
It is used by the machine running the following program.
```#include <stdio.h>
void bin(unsigned x) {    // another way to print a number in binary
if(x/2) bin(x/2);      // if we have not reached bit 0, carry on
putchar('0'+x%2);      // print the bit
}

void main() {
unsigned i;

for(i=0;i<256;i++) {   // print 0..255 in binary
bin(i);             // and as signed and unsigned chars
printf(" %d %d \n",(signed char)i,(unsigned char)i);
}
}
```

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.
```#include <stdio.h>

void main() {
signed char sc;
unsigned char uc;

uc=15;
uc=uc-23;
printf("15-23=%u\n",uc);
uc=200;
uc=uc+100;
printf("200+100=%u\n",uc);
sc=110;
sc=sc+40;
printf("110+40=%d\n",sc);
sc=-100;
sc=sc-150;
printf("-100-150=%d\n",sc);

} ```

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

Signed Integer using two's complement with 16,32 or 64 bits of precision (implementation dependent, 32 is most common).
unsigned
Unsigned integer with 16,32 or 64 bits or precision.
long
Signed Integer using two's complement with 32 or 64 bits of precision.
unsigned long
Unsigned Integer using two's complement with 32 or 64 bits of precision.
char
Signed Integer using two's complement with 8 bits of precision.
unsigned char
Unsigned Integer with 8 bits of precision.

## 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.