DigitHelm

Two's Complement Calculator

Convert integers to two's complement binary representation and back.

Conversion Mode

Bit Width

Valid range (8-bit signed): -128 to 127

What Is the Two's Complement Calculator?

Two's complement is the universal standard for representing signed integers in modern processors. Unlike sign-magnitude (which has two representations of zero), two's complement gives a unique bit pattern to every integer in the range and makes addition/subtraction hardware trivially simple, the same circuit handles both positive and negative operands.

The name comes from the arithmetic identity: the two's complement of n equals 2N − n, where N is the bit width. For an 8-bit value, −5 is stored as 256 − 5 = 251 (0xFB). Adding +5 and 0xFB in 8-bit arithmetic yields 256, which overflows back to 0, exactly the correct result for 5 + (−5).

Why two's complement replaced sign-magnitude

Modern CPUs use two's complement because a single adder circuit handles both addition and subtraction without special-casing the sign bit. Operations like comparisons, shifts, and multiplications all work naturally. Sign-magnitude required separate logic for negative numbers, wasting transistors and slowing clock speeds.

Formula

Bit-Width Reference, Range & Overflow Bounds

BitsMin (signed)Max (signed)Unsigned maxHex bytes
4−87150xF
8−1281272550xFF
12−2 0482 0474 0950xFFF
16−32 76832 76765 5350xFFFF
24−8 388 6088 388 60716 777 2150xFFFFFF
32−2 147 483 6482 147 483 6474 294 967 2950xFFFFFFFF
64−9.22 × 10¹⁸9.22 × 10¹⁸1.84 × 10¹⁹0xFFFFFFFFFFFFFFFF

How to Use

  1. 1Select your bit width (4, 8, 12, 16, 24, or 32 bits).
  2. 2Choose a mode: Decimal → Binary converts a signed integer to its binary representation; Binary → Decimal parses a raw bit string; Hex → Binary accepts hexadecimal input.
  3. 3Enter your value in the input field. The calculator accepts negative decimals and validates range automatically.
  4. 4Click Calculate (or press Enter) to see the two's complement bit pattern, sign-magnitude, one's complement, octal, and hex representations.
  5. 5The visual bit display color-codes each bit: the sign bit is highlighted in red, set bits (1) in brand color, and zero bits in muted gray, making the MSB instantly recognizable.
  6. 6Overflow is flagged if the entered decimal falls outside the signed range for the chosen bit width.

Example Calculation

Example: Represent −45 in 8-bit two's complement

Step 1, Write +45 in binary (8 bits): 45 = 00101101 Step 2, Flip all bits (one's complement): 00101101 → 11010010 Step 3, Add 1: 11010010 + 1 = 11010011 Result: −45 (decimal) = 11010011 (binary) = 0xD3 (hex) Verify: 0xD3 = 211; 211 − 256 = −45 ✓

The sign bit (MSB) is 1, confirming a negative value. To decode any negative two's complement number, flip bits and add 1, the same procedure works in reverse.

Understanding Two's Complement

Two's complement is the signed integer encoding used by virtually every processor manufactured since the 1970s, from desktop CPUs to microcontrollers. Understanding it is essential for low-level programming, embedded systems, cryptography, and debugging unexpected integer behaviour in languages like C, Java, and Rust.

The encoding works by reserving the most-significant bit (MSB) as a sign bit with weight −2N−1, while all other bits carry their usual positive weights. For an 8-bit number, bit 7 contributes −128 and bits 6–0 contribute 0–127. This makes the range asymmetric: −128 to +127 rather than ±127.

One crucial advantage over sign-magnitude encoding is that addition hardware needs no special-casing: the same binary adder that computes 3 + 4 also correctly computes 3 + (−4) and (−3) + (−4). The carry out of the MSB is simply discarded, and the remaining bits hold the correct two's complement result. This simplicity is why every modern ISA, x86, ARM, RISC-V, MIPS, adopted two's complement.

When debugging bit-level bugs, this calculator's visual bit display lets you see exactly which bits are set, immediately spot the sign bit, and verify overflow conditions before they reach production.

Frequently Asked Questions

Why does two's complement have one more negative value than positive?

An N-bit two's complement number ranges from −2^(N−1) to 2^(N−1)−1. For 8 bits that is −128 to +127.

  • Zero takes one slot on the positive side, leaving one extra negative value.
  • −128 in 8-bit has no positive counterpart, negating it overflows back to −128.
  • This is why abs(INT_MIN) causes undefined behavior in C/C++.
  • The asymmetry is a fundamental property, not a quirk of the implementation.

How do I convert a two's complement binary string back to decimal by hand?

The procedure depends on the sign bit (MSB):

  • MSB = 0: convert binary to decimal directly (positive value).
  • MSB = 1: flip all bits, add 1, convert to unsigned decimal, then negate.
  • Example, 11010011: flip → 00101100, add 1 → 00101101 = 45, result = −45.
  • Shortcut: treat the MSB as −2^(N−1) and sum remaining bits normally.

What is the difference between one's complement and two's complement?

  • One's complement: flip all bits. Has two representations of zero (+0 and −0).
  • Two's complement: flip all bits then add 1. Only one zero representation.
  • Two's complement addition wraps cleanly, no end-around carry needed.
  • All modern CPUs (x86, ARM, RISC-V) use two's complement exclusively.
  • One's complement still appears in checksums (e.g., IPv4, TCP/UDP headers).

How does sign extension work when widening a two's complement number?

Sign extension preserves the numeric value when widening a register:

  • Copy the MSB (sign bit) into every new high-order bit position.
  • Negative example: 8-bit 11111011 (−5) → 16-bit 1111111111111011 (still −5).
  • Positive example: 8-bit 00000101 (+5) → 16-bit 0000000000000101 (still +5).
  • Truncation (narrowing) discards high bits and may overflow, the inverse is not always safe.

What causes integer overflow in two's complement arithmetic?

  • Positive + Positive = Negative result → overflow (e.g., 127 + 1 = −128 in 8 bits).
  • Negative + Negative = Positive result → overflow (e.g., −128 + (−1) = 127 in 8 bits).
  • CPU sets the overflow (V) flag in the ALU status register when this happens.
  • In C/C++, signed overflow is undefined behavior, use INT_MAX checks or __builtin_add_overflow.
  • Python integers are arbitrary-precision and never overflow.

Related Calculators