Two's Complement

Two’s Complement Basics

In two’s complement for an n-bit signed integer:

  • Range: -2^(n-1) to 2^(n-1) - 1 (note the asymmetry)
  • The Most Significant Bit (MSB) is the sign bit:
    • 0 = positive (or zero)
    • 1 = negative
  • The Negation Rule: Geting the negative of a number:
    • Step 1: nvert all bits (NOT operation: flip 0→1, 1→0)
    • Step 2: Add 1

Two’s Complement Formula

In two’s complement, the MSB has a negative weight:

  • MSB = 1 contributes -2³¹ to the total value
  • All other bits contribute their positive powers of 2

For a 32-bit number, the value is:

1
2
3
Value = -(bit₃₁ × 2³¹) + (bit₃₀ × 2³⁰) + (bit₂₉ × 2²⁹) + ... + (bit₁ × 2¹) + (bit₀ × 2⁰)
        \_____________/   \___________________________________________________/
         Negative weight              Positive weights

Simple Example (4-bit):

1
2
3
4
1101 in 4-bit two's complement:
= -(1 × 2³) + (1 × 2²) + (0 × 2¹) + (1 × 2⁰)
= -8 + 4 + 0 + 1
= -3
Licensed under CC BY-NC-SA 4.0
Last updated on Dec 03, 2025 00:00 UTC