Twos Comp PDF
Twos Comp PDF
Michael Burrell
0 0 1 1
0 0 1 1
Step 2: The leftmost bit is the sign bit. 0 means “positive” and 1
means “negative”
Making a negative number in 1’s complement
0 0 1 1
1 0 1 1
1 1 1 1
1 1 0 1
1 1 0 0
1 1 0 0
Value
0 0 0 0
1 1 1 -0
0 0 1 1
1 1 0 -1
0 1 0 2
1 0 1 -2
0 1 1 3
1 0 0 -3
1’s complement in 3 bits
Value
0 0 0 0
1 1 1 -0
0 0 1 1
1 1 0 -1
0 1 0 2
1 0 1 -2
0 1 1 3
1 0 0 -3
Value
0 0 0 0
1 1 1 -0
0 0 1 1
1 1 0 -1
0 1 0 2
1 0 1 -2
0 1 1 3
1 0 0 -3
I It works!
I It is awkward in a couple ways:
I It has a representation for negative zero
I Making an electronic circuit to do addition and subtraction is
complex
1’s complement
I It works!
I It is awkward in a couple ways:
I It has a representation for negative zero
I Making an electronic circuit to do addition and subtraction is
complex
I But, it’s close to what we want. . .
2’s complement
0 0 1 1
1 1 0 0
1 1 0 0
Step 3: Add 1
Making a negative number in 2’s complement
1 1 0 1
Step 3: Add 1
Making a negative number in 2’s complement
1 1 0 1
Value
0 0 0 0
0 0 1 1
1 1 1 -1
0 1 0 2
1 1 0 -2
0 1 1 3
1 1 0 -3
1 0 0 -4
2’s complement in 3 bits
Value
0 0 0 0
0 0 1 1
1 1 1 -1
0 1 0 2
1 1 0 -2
0 1 1 3
1 1 0 -3
1 0 0 -4
0 1 1 1 7
+ 0 1 0 1 5
=
0 1 1 1 7
+ 0 1 0 1 5
=
0 1 1 1 7
+ 1 0 1 0 5
=
0 1 1 1 7
+ 1 0 1 1 -5
=
0 1 1 1 7
+ 1 0 1 1 -5
=
0 1 1 1 7
+ 1 0 1 1 -5
= 0
1 1
0 1 1 1 7
+ 1 0 1 1 -5
= 1 0
1 1 1
0 1 1 1 7
+ 1 0 1 1 -5
= 0 1 0
1 1 1 1
0 1 1 1 7
+ 1 0 1 1 -5
= 0 0 1 0 2
1 1 1 1
0 1 1 1 7
+ 1 0 1 1 -5
= 0 0 1 0 2
I To convert from positive to negative, flip all the bits and add
1!
I To convert from negative to positive, flip all the bits and add
1!
I “Flip all the bits” is also called “1’s complement”
I For n bits, we can represent all integers from −2n to 2n − 1
How to do 2’s complement
I To convert from positive to negative, flip all the bits and add
1!
I To convert from negative to positive, flip all the bits and add
1!
I “Flip all the bits” is also called “1’s complement”
I For n bits, we can represent all integers from −2n to 2n − 1
I Addition is very easy. Adding two positive numbers, two
negative numbers, one positive and one negative, etc., all
works the same!