InfyCalculator
💻

Two's Complement Calculator

Convert a signed decimal number to two's complement binary (4 to 32 bits) with the invert-and-add-one steps, one's complement, sign-magnitude and hex — or decode a two's complement binary back to decimal.

Direction
Decimal number
Bit width
Two's complement binary (for → decimal)
Loading calculator…

How it works

Negative n in w bits: write |n| in binary, invert every bit, add 1 (equivalently 2ʷ − |n|) · decoding: value = −b₍w−1₎·2^(w−1) + Σ bᵢ·2ⁱ · range: −2^(w−1) … 2^(w−1) − 1 · one's complement = inverted bits only; sign-magnitude = sign bit + |n|

Two's complement is how virtually every processor stores signed integers: the most significant bit has a negative weight, so the same adder circuit handles positive and negative numbers and there is only one zero. To negate a number you invert all bits and add one. The calculator shows that recipe step by step, plus the older one's complement and sign-magnitude encodings for comparison, and the unsigned value the same bit pattern would have — useful when reading register dumps or C casts.

Worked example

−45 in 8 bits: 45 = 0010 1101 → invert → 1101 0010 → add 1 → 1101 0011 (0xD3). Read as unsigned, the same bits are 211 = 256 − 45. One's complement would be 1101 0010 and sign-magnitude 1010 1101. Going back, 11010011 has sign bit 1, so invert (0010 1100) and add 1 (0010 1101 = 45): the value is −45.

Sources: Two's complement — Wikipedia · Signed number representations — Wikipedia

Frequently asked questions

How do I find the two's complement of a negative number?

Write the positive value in binary with the full bit width, flip every bit (0 ↔ 1), then add 1. Shortcut: copy bits from the right up to and including the first 1, then invert all bits to its left.

What is the range of an 8-bit two's complement number?

−128 to +127. In general w bits hold −2^(w−1) to 2^(w−1) − 1: 16 bits −32,768 to 32,767, 32 bits −2,147,483,648 to 2,147,483,647. There is one more negative value than positive because zero takes a slot on the positive side.

Why does −128 have no one's complement or sign-magnitude form in 8 bits?

Those encodings have two zeros (+0 and −0) and so can only reach −127. Two's complement uses the spare pattern 1000 0000 for −128, which is why it is the standard.

How do I know if a binary number is negative?

Only if you know it is a signed two's complement value of a fixed width: then a leading 1 means negative. The same bits 1101 0011 are 211 as an unsigned byte and −45 as a signed byte — the interpretation, not the bits, decides.

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

One's complement just inverts the bits (−45 = 1101 0010) and has both +0 and −0; adding needs an end-around carry. Two's complement adds 1 after inverting (−45 = 1101 0011), has a single zero and lets ordinary binary addition work for signed numbers.

Related calculators

This calculator is for educational purposes. Double-check important results before acting on them.