Introduction

The most popular method for representing negative numbers in a binary system.

The Representation

Assume we're dealing with bytes. In a 2's complement system, positive numbers are represented as for a system without negative numbers. For example,

27 decimal = 0001 1011

Under the 2's complement rules, to represent -27, we invert each bit and add one. Thus -27 is

-27 decimal = 1110 0101

As you can see, the left most bit effectively acts as a sign bit (1 is negative, 0 is positive). This means that the effective range of a signed byte under the 2's complement system is -128 to 127 (-128 can be represented as 1000 0000).

Arithmetic Operations

The nice thing about the 2's complement system is that it makes it easy to do arithmetic operations on negative numbers just as you would if you were only using positive numbers (i.e. unsigned integers), unlike other representation systems. For instance, let's add the 27 and -27 from above.

  0001 1011 (27)
+ 1110 0101 (-27)
  ---------
  0000 0000 (0)

Starting from the rightmost (i.e. least significant) bit, 1+1 means the answer is set to 0 and we carry 1 leftwards. The next bit has a 1 for the positive representation, and combined with the carry over this creates another carry but leaves a zero behind. Doing this for the rest of the sum and discarding the end carry, we see that the result is zero.

Here's another example

  0011 0101 (53)
+ 1110 1110 (-18)
  ---------
  0010 0011 (35)

Multiplication and division under this system also follow the same computational rules as for their unsigned operations.