Two’s complement is a method of representing signed integers in binary.

For a -bit long bit string in two’s complement, a at the first bit (or the -th from the right) represents the most negative value possible () for this string. The rest of the bits represent the original (positive) values assigned to them in binary.

The signed integer represented by the binary string is therefore: where is the -th bit counting from the right and is the length of the binary string.

Two’s complement for 8-bit signed integers:

DecimalTwo’s Complement Binary
00000 0000
10000 0001
20000 0010
1260111 1110
1270111 1111
−1281000 0000
−1271000 0001
−1261000 0010
−21111 1110
−11111 1111

To negate a signed number in two’s complement, flip all bits and add one (~n + 1).

Therefore, to convert a negative decimal number to two’s complement:

  • Find the absolute value of the number in binary
  • Negate the absolute value (flip all bits and add one—“taking the two’s complement”).