msb | 10010010 | lsbFour systems for representing negative numbers:

- Signed Magnitude
- One's Complement
- Excess n Notation
- Two's Complement

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

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

Add n to the number. n is usually 2

e.g.:4=10000100, -4=011111100 (-4+128=124)

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.

- Positive numbers are the same as the unsigned representation.
- Unsigned addition and subtraction works for signed numbers.
- There is only one representation for zero.

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.

- For unsigned numbers, crossing the top of the diagram.
- For signed numbers, crossing the bottom of the diagram.

10-11=255.....Wrong.

250+9=3..........Wrong again.

These cases cause a

120+10=-126.....Wrong.

-100-29=127.....Wrong again.

These cases cause an

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 integer with 16,32 or 64 bits or precision.

- Signed Integer using two's complement with 32 or 64 bits of precision.

- Unsigned Integer using two's complement with 32 or 64 bits of precision.

- Signed Integer using two's complement with 8 bits of precision.

- Unsigned Integer with 8 bits of precision.

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.