Computer Science
Advanced

Two's Complement

Represents negative integers in binary by inverting bits and adding one.

Formula

−N=¬N+1-N = \lnot N + 1

Variables

NBinary number

Example

In 8-bit two’s complement, −5 is ~00000101 + 1 = 11111011.

How it works

In two’s complement arithmetic, a negative number is stored by taking the bitwise inverse of the positive magnitude and then adding 1. This works because, for a fixed bit-width, the result is the additive inverse modulo 2^n: when you add a number to this transformed version, the carry wraps around and produces 0 in the low n bits. The formula only makes sense relative to a chosen register size, since overflow beyond that width is intentionally discarded.

When to use it

Use this when converting signed integers to binary for CPUs, digital circuits, low-level debugging, or manually checking machine code behavior. It is especially important when reasoning about overflow, subtraction, or how signed values are represented in a fixed number of bits.

Worked example, step by step

Find the 8-bit two’s complement representation of −18. Step 1: Write +18 in 8-bit binary: 00010010 Step 2: Invert every bit: 11101101 Step 3: Add 1: 11101101 + 1 = 11101110 So, −18 = 11101110 in 8-bit two’s complement.

Common mistakes to avoid

  • Forgetting that the bit-width is fixed, so extra carry bits are discarded
  • Inverting the bits without adding 1, which gives one’s complement instead
  • Using the formula on a decimal number directly instead of first writing its binary form

Did You Know?

Two's complement lets computers subtract by adding — which is why almost every CPU uses it for signed integers.

Share this formula

Reviewed & maintained by the FormulaWon editorial teamLast reviewed September 2026