# Two's Complement Notation

Two's complement number representation is used for signed numbers on most modern computers. This notation allows a computer to add and subtract numbers using the same operations (thus we do not need to implement adders and subtractors). We can characterize two's complement notation as:
• A fixed number of bits are used to represent numbers
• The most significant bit is called the sign bit
• This same notation is used to represent both positive and negative numbers

### Positive numbers represented normally

Example: Using a 4 bit representation 5 in 2's complement
0101
Example: Using an 8 bit representation 5 in 2's complement
0000 0101
Example: Using an 8 bit representation 24 base 16
0010 0100

### Negative numbers

Are represented using a 2's complement form. To obtain the 2's complement of a number:
1. Complement the bits
2. Add one to the result
Examples: (4 bits)

Represent -6 in 2's complement

```+6         0110
complement 1001
-6 =       1010
```
Represent -3 in 2's complement
```+3         0011
complement 1100
-3 = 	   1101

```
Example: (5 bits)

Represent -13 in 2's complement

```+13        01101
complement 10010
-3 =	   10011

```

### Addition and subtraction in 2's complement notation

Is performed by doing the simple binary addition of the two numbers. Subtraction is accomplished by first performing the 2's complement operation on the number being subtracted then adding the two numbers.
Examples: 5 bits
```   8       01000
+4       00100
--       -----
12       01100

-8       11000
+ -4       11100
--       -----
-12       10100

8       01000
+ -4       11100
--       -----
4       00100
```
Example: 8 bits
```   25   0001 1001               19
+ -29   1110 0011               E3
---   ---------
-4   1111 1100               FC
```

### Overflow

Since we are working with numbers contained in a fixed number of bits, we must be able to detect overflow following an operation.
1. No overflow occurs when the value of the bit carried into the most significant bit is the same as the value carried out of the most significant bit.
2. Overflow occurs when the value of the bit carried into the most significant bit is not the same as the bit carried out of the most significant bit.
Examples: 4 bits
```

6                   0110
+ 1                   0001
-----                 ------
7                   0111

7                   0111
+ 1                   0001
-----                  ------
8                   1000
```
but, in the second case is 8 the correct answer?