Use one bit for sign, 7 bits for number

Example: -1 (in an 8-bit system) could be 1000 0001 (base 2)

**2's complement** (most often used in computing)

Example: the 2's complement number representing -1 (in an 8-bit system) would be 1111 1111 (base 2)

- 2 + 998In the second example, the correct result is just the +001;+ 3+ 003+ 1 1 001

Do subtraction as addition by using complements, i.e.

**A - B = A + (-B)**

**Important:** It is easier to compute -B and add than to subtract directly.

Example:

- 005 + 995Note that 995 and 997 were added in the normal fashion, the overflow was ignored, and the result was 992, which can be converted from the complement (or odometer) system back to -8, the correct answer.+ 003+ 997+ 0081992

There are 256 numbers in an 8-bit fixed length 2's complement number system.signed 3-bit complement+3 003 +2 002 +1 001 0 000 -1 999 -2 998 -3 997 -4 996 -5 995 -6 994 -7 993 -8 992decimal 2's complement binary+127 01111111 +126 01111110 +125 01111101 ... ........ +2 00000010 +1 00000001 0 00000000 -1 11111111 -2 11111110 ... ........ -127 10000001 -128 10000000

Why are these numbers called 2's complement?

**M - N = M + (-N)**

where -N is the complement of N.

Example:

decimal + 1 = 00000001which does not equal 2^8, or 256; ignore the overflow, and the correct answer is zero.- 1=+ 111111110 = 1 00000000

Now we need an easy way to do 2's complement operations.

Example:

What is the representation of -27 (base 10) in 2's complement 8-bit notation?

2^8 - 1 11111111 - 27This approach is necessary because:- 0001101111100100 corresponds to flipping all the bits; also known as 1's complement add 1+ 00000001result 11100101 -27 in 2's complement representation

- Fixed-length nature of numbers stored in a computer
- More computationally efficient to implement a fast adder than an adder
**and**a subtractor

- Addition: To compute N1 + N2, add N1 to N2
- Subtraction: To compute N1 - N2, add N1 to -N2

Examples:

decimal binary hex 11 = 00001011 = 000BHow we got the 2's complements of -11 and -21:+ 21=+ 00010101=+ 001532 = 00100000 = 0020 21 = 00010101 = 0015- 11=+ 11110101=+ FFF510 = 00001010 = 000A 11 = 00001011 = 000B- 21=+ 11101011=+ FFEB- 10 = 11110110 = FFF6 - 11 = 11110101 = FFF5- 21=+ 11101011=+ FFEB- 32 = 1 11100000 = FFE0

11 00001011 ~11 - 11110100 1's complement+ 1+ 00000001add 1 -11 11110101 2's complement representation

- Store N.
- Obtain ~N, the 1's complement of N, by replacing (bit by bit) every 0 with a 1 and vice versa in the number's binary representation.
- Add 1 and ignore any carry after the eighth bit.

**Note:** This algorithm works in hex by replacing each digit x with its hex complement, i.e. 15 - x. Example: The hex equivalent of 11 is $000B; its hex complement is then $FFF4, where each digit was computed as $F - x. Adding 1 to $FFF4 gives the result $FFF5 for the 2's complement of 11. (Using a dollar sign ($) before a number indicates that it is base 16.)

Example:

In binary:

N 0000 0110 0100 0111 ~N - 1111 1001 1011 1000 1's complementIn hex:+1+ 0000 0000 0000 0001add 1 -N 1111 1001 1011 1001 2's complement representation

N 0647 ~N - F9B8 1's complementA calculator will always give you the 2's complement directly.+1+ 0001add 1 -N F9B9 2's complement representation

The Most Significant Bit (MSB) is the binary digit corresponding to the largest power of 2 and is always 1 for a negative 2's complement number. As a result it is often called the sign bit.

In 2's complement, -0 = +0. Technically, 0 is the complement of itself.

N 00000000 ~N - 11111111 1's complement+1+ 00000001add 1 -N 00000000 2's complement representation

Adding signed numbers can easily exceed these limits.

first digit 0011 $3000This result, from adding two positive numbers in the number system, results in $9000, which is larger than $7FFF, the largest allowed positive number. In fact, $9000 equals 1001 0000 0000 0000, which is a negative number. The sign of the result is different from that of the operands.0110$60001001 $9000

**Rule:** If there is a sign change in adding two positive 2's complement numbers, then **overflow** has occurred.

We can generalize these rules to signed addition and subtraction.

To extend a 2's complement number to a greater number of binary digits, you just continue the sign bit to the left.decimal 2's complement binary3-bit4-bit8-bit+3 011 0011 00000011 +2 010 0010 00000010 +1 001 0001 00000001 0 000 0000 00000000 -1 111 1111 11111111 -2 110 1110 11111110 -3 101 1101 11111101 -4 100 1100 11111100

Examples:
__Extend $9B to 16 bits.__

$9B = 1001 1011 = 1111 1111 1001 1011 = $FF9B

$5F = 0101 1111 = 0000 0000 0101 1111 = $005F

43A0 43A0Note that the 8-bit $9B and the 16-bit $FF9B both represent -101 in their respective number systems.9BFF9Bneed to sign extend; you can't just add zeros. ???? 1 433B

Extracted from http://www.cwru.edu/4208000/cse/eeap/282/03_number_systems.html.