Eroxl's Notes
Two's Complement

Two's complement is the most common way to represent negative numbers and subtract numbers in binary. Two's complement uses the largest digit as the sign to represent whether the number is negative or positive (1 being negative and 0 being positive).

Procedure

The following is the procedure for obtaining the two's complement of a given negative number in binary digits:

  1. Starting with the positive binary representation of the number, with the leading bit being 0.
  2. Invert all the bits – changing every 0 to 1, and every 1 to 0. This can be thought of as the negation of the original number.
  3. Add 1 to the entire inverted number, ignoring any overflow. Accounting for overflow will produce the wrong value for the result.

Examples

Example 1

Calculate the two's complement binary representation of the decimal number -6.

Example 2

Calculate the two's complement binary representation of the decimal number -19.

Alternative Method

Alternatively the two's complement of a given number can be calculated by computing the equation where is the number being converted, is the number of bits and represents the modulo operator. The output of the equation is then written as an unsigned binary number which gives the two's complement of the original number .

Example

Calculate the Two's Complement Binary Representation of the Decimal Number -2 Using a Modulo Operation and only 3 bits

The two's complement of 2 is 110 given only 3 bits, therefore -2 can be written as 110 using two's complement notation.

Converting Back to Decimal

To convert the two's complement of a given binary number first repeat the original procedure to find the absolute number then convert it to decimal and then negate it.

Example

Determine the decimal representation of the two's complement binary number .

Difference in Ranges Between Signed and Unsigned Binary Numbers

Because signed binary numbers using two's complement use the first bit to represent the sign of the number the range of values will be different.

The range of numbers being able to be represented by a non-signed binary number being the integer interval , where is the number of bits. Conversely it is for a signed binary number with two's complement.