Two’s Complement Calculator

Convert between decimal and two’s complement binary with a fixed bit width.

Twos Complement Calculator

Convert between decimal and twos complement binary representation with step-by-step explanations

Input & Settings

Calculate twos complement by flipping bits and adding 1

Result

Calculation output and explanation

Enter a value and click Calculate to see the result

Twos Complement Information

How Twos Complement Works

  • • Positive numbers: same as binary representation
  • • Negative numbers: flip all bits and add 1
  • • Range for n bits: -2^(n-1) to +(2^(n-1) - 1)
  • • Only one representation for zero
  • • Standard in modern computer systems

Example (4-bit)

+3: 0011
-3: 1101 (flip bits + add 1)
+0: 0000
Range: -8 to +7

How it works

Two’s complement is the usual way CPUs store signed integers. Positive values look like ordinary binary. Negatives: invert all bits (ones’ complement), then add 1. The MSB is the sign bit (1 = negative). Zero has one encoding: all zeros.

n-bit range is −2ⁿ⁻¹ … 2ⁿ⁻¹−1 (one more negative than positive). Example 4-bit: −8 … +7.

Why it stuck

  • Same adder hardware for signed and unsigned (mod 2ⁿ)
  • Single zero—no +0/−0 mess like sign-magnitude
  • Negate by ~x + 1

Overflow wraps: INT_MAX + 1 becomes INT_MIN on typical wraparound. In C/C++, signed overflow is undefined behavior—do not rely on wrap for portable code.

Quick 8-bit example

+5 → 00000101
−5 → invert 11111010, add 1 → 11111011
Check: 00000101 + 11111011 = 00000000 (mod 256)

Related tools

Frequently Asked Questions

Vs ones’ complement?

Ones’ complement only flips bits and has two zeros. Two’s adds one more step and is what nearly all modern ISAs use for integers.

Why does bit width matter?

The same pattern means different values at different widths. 1111 is −1 in 4-bit two’s complement, not −1 as an 8-bit value unless you sign-extend.

Arithmetic right shift?

Fills with the sign bit so negatives stay negative when dividing by two (toward −∞ in typical two’s implementations—check your language).

Related tools

Related tools