2s Complement
We have seen how 1s complement is useful for addition/subtraction operation. However, it still suffers from redundant 0. What we want now is to remove the redundant 0. Although we can remove either, it makes more sense to remove -0 (or rather (1111...11)1s) instead of +0 (or rather 0000...00)1s). The question is how.
If we want to ensure that the left-most bit is still the "sign bit" and that arithmetic operation works similar to 1s complement, then our choice of value for 1111...11 is rather limited. The most sensible choice is to make this the first negative number after -0, which is -1. This requires us to shift all representation by one.
Representation
As we need to perform the shift, the formula changes to:
-x = 2n-x
Although the shift seems to indicate that we have to subtract one (due to -0 now becoming -1), in terms of magnitude, we add one.
Another way to remember this is simply:
Negative 2s Complement
Flip all the bits, then add 1
8 bits 2s Complement
Consider 8 bits 2s complement number. How do we represent (-12)10 in this representation?
- Find the binary representation of (12)10: (00001100)2.
- Invert all the bits: (11110011)1s.
- Add one: (11110100)2s.
- Find the "equivalent" number: -12 = 28-12 = 244.
- Find the binary representation of (244)10: (11110100)2.
- Change the base: (11110100)2s.
Quick Quiz
Consider 8 bits 2s complement number. How do we represent (-80)10 in this representation?
(10110000)2s
steps omitted...
Properties
2s complement has all the nice properties of 1s complement without the redundant 0. We will skip most of this section and will only mention the range and arithmetic.
Range
Kind | Representation | Decimal |
---|---|---|
Largest | 0111...11 | (+2n-1-1)10 |
Smallest | 1000...00 | (-2n-1)10 |
0 | 0000...00 | (0)10 |
We use the entire possible 2n possible numbers.
Arithmetic
The arithmetic of 2s complement is even simpler than 1s complement because of the absence of redundant 0. In particular, we do not need to add 1 when we cross the point. The overflow rule is also similar to 1s complement.
2s Complement Addition | |
---|---|
1 2 3 4 5 6 |
|
4 bit 2s Complement
1 2 3 4 |
|
1 2 3 4 5 6 |
|
1 2 3 4 5 6 |
|
1 2 3 4 5 6 |
|
1 2 3 4 |
|
1 2 3 4 5 6 |
|
0111
has different sign from the operands.
1 2 3 4 |
|
1011
has different sign from the operands.
Quick Quiz
Calculate the result of the following computation using 4 bits 2s complement. Identify if there is any overflow.
- -1 - 7 = ?
- 3 - -5 = ?
- 7 - 0 =?
-
-8
Steps
- Represent as addition: -1 - 7 = (-1) + (-7)
- Convert the number into 2s complement:
- (-1)10 = (1111)2s
- (-7)10 = (1001)2s
-
Binary addition
1 2 3 4
1 111 -1 1 001 -7 ----- + --- + 11 000 -8
-
Truncate
1
11 000 --> 1 000
-
Overflow check:
- The operands have the same sign
- The result has the same sign as the operands
- There is no overflow
-
-8 (overflow)
Steps
- Represent as addition: 3 - -5 = 3 + 5
- Convert the number into 2s complement:
- (3)10 = (0011)2s
- (5)10 = (0101)2s
-
Binary addition
1 2 3 4
0 011 +3 0 101 +5 ----- + --- + 1 000 +8
-
Overflow check:
- The operands have the same sign
- The result has different sign from operands
- There is an overflow
-
+7
Steps
- Represent as addition: 7 - 0 = 7 + 0
- Convert the number into 2s complement:
- (7)10 = (0111)2s
- (-0)10 = (0000)2s (there is no -0)
-
Binary addition
1 2 3 4
0 111 +7 0 000 +0 ----- + --- + 0 111 +7
-
Overflow check:
- The operands have the same sign
- The result has the same sign as the operands
- There is no overflow