Skip to content

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?

  1. Find the binary representation of (12)10: (00001100)2.
  2. Invert all the bits: (11110011)1s.
  3. Add one: (11110100)2s.
  1. Find the "equivalent" number: -12 = 28-12 = 244.
  2. Find the binary representation of (244)10: (11110100)2.
  3. 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

2s Complement

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
# input : number A in 1s complement
#         number B in 1s complement
# output: number C = A + B
C := A + B            # binary addition
check_overflow(A,B,C) # overflow rule
C := truncate(C)

4 bit 2s Complement

1
2
3
4
0 011     +3
0 100     +4
----- +   -- +
0 111     +7
There is no overflow because the resulting sign bit is the same as the operands.

1
2
3
4
5
6
 0 101     +5
 1 011     -5
 ----- +   -- +
10 000     -0
 -----
 0 000  // truncate
There is no overflow because the operands have different signs. This will never result in overflow. Also, there is no longer -0 now.

1
2
3
4
5
6
 1 110     -2
 1 010     -6
 ----- +   -- +
11 000     -8
 -----
 1 000  // truncate
There is no overflow because the resulting sign bit is the same as the operands.

1
2
3
4
5
6
 0 110     +6
 1 101     -3
 ----- +   -- +
10 011     +3
 -----
 0 011  // truncate
There is no overflow because the operands have different signs. This will never result in overflow.

1
2
3
4
0 100     +4
1 001     -7
----- +   -- +
1 101     -3
There is no overflow because the operands have different signs. This will never result in overflow.

1
2
3
4
5
6
 1 101     -3
 1 010     -6
 ----- +   --- +
10 111     -9
 -----
 0 111  // truncate
There is overflow because the final result 0111 has different sign from the operands.

1
2
3
4
0 101     +5
0 110     +6
----- +   --- +
1 011     +11
There is overflow because the final result 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. -1 - 7 = ?
  2. 3 - -5 = ?
  3. 7 - 0 =?
  1. -8

    Steps
    1. Represent as addition: -1 - 7 = (-1) + (-7)
    2. Convert the number into 2s complement:
      • (-1)10 = (1111)2s
      • (-7)10 = (1001)2s
    3. Binary addition

      1
      2
      3
      4
       1 111     -1
       1 001     -7
       ----- +   --- +
      11 000     -8
      
    4. Truncate

      1
      11 000 --> 1 000
      
    5. Overflow check:

      • The operands have the same sign
      • The result has the same sign as the operands
      • There is no overflow
  2. -8 (overflow)

    Steps
    1. Represent as addition: 3 - -5 = 3 + 5
    2. Convert the number into 2s complement:
      • (3)10 = (0011)2s
      • (5)10 = (0101)2s
    3. Binary addition

      1
      2
      3
      4
       0 011     +3
       0 101     +5
       ----- +   --- +
       1 000     +8
      
    4. Overflow check:

      • The operands have the same sign
      • The result has different sign from operands
      • There is an overflow
  3. +7

    Steps
    1. Represent as addition: 7 - 0 = 7 + 0
    2. Convert the number into 2s complement:
      • (7)10 = (0111)2s
      • (-0)10 = (0000)2s (there is no -0)
    3. Binary addition

      1
      2
      3
      4
      0 111     +7
      0 000     +0
      ----- +   --- +
      0 111     +7
      
    4. Overflow check:

      • The operands have the same sign
      • The result has the same sign as the operands
      • There is no overflow