Two's Complement Calculator

Convert a decimal integer to its n-bit two's complement binary form, or decode a two's complement binary value back to decimal — includes one's complement, the valid signed range, and the sign bit.

Quick Facts

Negative encoding
two's complement = 2^n + x
Equivalent to inverting every bit of the positive value and adding 1.
Signed range (n bits)
-2^(n-1) to 2^(n-1) - 1
For 8 bits: -128 to 127 — asymmetric because there's only one zero.
Sign bit
Most significant bit (MSB)
MSB = 0 means non-negative; MSB = 1 means negative.
One's complement
(2^n - 1) - value
Bitwise NOT of the n-bit pattern; two's complement is one's complement + 1.

Your Results

Calculated
Result
-
Two's complement binary, or decoded decimal
One's Complement
-
Bitwise NOT of the n-bit pattern
Signed Range
-
-2^(n-1) to 2^(n-1) - 1 for this bit width
Sign
-
Based on the most significant bit

Ready

Choose a conversion direction, enter a value and bit width, then press Calculate.

How Two's Complement Works

Two's complement is the standard way computers represent signed (positive and negative) integers in binary. Instead of reserving a bit purely as a "+/-" flag (sign-magnitude) or inverting bits alone (one's complement), two's complement encodes a negative number x, within an n-bit width, as 2n + x — its value modulo 2n. This single trick gives binary integers exactly one representation of zero and lets a processor add and subtract signed numbers with the same hardware it uses for unsigned numbers.

Formula and method

For a non-negative decimal value, the n-bit two's complement representation is just the ordinary binary form of the number, padded with leading zeros to n bits. For a negative value x (where -2n-1 ≤ x ≤ -1), compute the unsigned n-bit pattern as 2n + x, then convert that unsigned result to binary. In practice this is usually done with the "invert and add one" shortcut: write the binary form of |x|, flip every bit (0↔1) to get the one's complement, then add 1. Both methods produce the same bit pattern. To reverse the process — decoding an n-bit two's complement string back to decimal — read the bits as an unsigned binary number; if the leading (most significant) bit is 0, that unsigned value is the answer, and if it is 1, subtract 2n from the unsigned value to recover the negative result.

Worked example

Encode -76 in 8 bits: |−76| = 76 = 01001100 in binary. Invert every bit to get the one's complement, 10110011, then add 1 to get 10110100 — the two's complement of -76. Check it against the direct formula: 28 + (-76) = 256 - 76 = 180, and 180 in binary is indeed 10110100. Decoding it back: the leading bit is 1, so the value is negative; the unsigned value of 10110100 is 180, and 180 - 256 = -76, matching the original input.

Common sources of error

  • Wrong bit width: the same bit pattern means different decimal values at different widths — 10110100 is -76 as an 8-bit number but a large positive number if treated as 16 or 32 bits without sign-extension.
  • Off-by-one on the range: an n-bit two's complement number covers -2n-1 to 2n-1 - 1, not -2n-1 to 2n-1 — there is one more negative value than positive value because zero only uses one code.
  • Confusing one's complement with two's complement: one's complement is just the bitwise NOT; two's complement is one's complement plus 1, and only two's complement has a single representation of zero.
  • Overflow: adding two n-bit two's complement numbers whose true sum falls outside the representable range wraps around silently — always check the operands against the signed range first.

Applications

Two's complement is used almost universally in digital hardware and programming languages (C, Java, Python's fixed-width integer types, CPU registers) to store signed integers, because addition, subtraction, and multiplication of two's complement numbers use the exact same binary adder/multiplier circuits as unsigned arithmetic — the hardware does not need a separate "subtract" circuit or special-case logic for the sign. It also shows up when reading raw binary or hexadecimal dumps (network packets, file formats, machine code) where a signed field must be decoded correctly, and in embedded systems and low-level debugging where engineers convert between hex, binary, and signed decimal by hand.

Frequently Asked Questions

How do you convert a negative decimal number to two's complement binary?
Write the binary form of the positive value, invert every bit (0 becomes 1, 1 becomes 0), then add 1. Equivalently, compute 2n + x for an n-bit width, since two's complement represents x as its value modulo 2n.
What range of values can an n-bit two's complement number represent?
An n-bit two's complement number ranges from -2n-1 to 2n-1 - 1. For 8 bits that is -128 to 127; the range is asymmetric because there is only one representation of zero.
How do you decode a two's complement binary number back to decimal?
Read the bits as an unsigned binary number. If the most significant bit (MSB) is 0, that unsigned value is the answer. If the MSB is 1, subtract 2n from the unsigned value to get the negative signed result.
Why is two's complement used instead of sign-magnitude or one's complement?
Two's complement has a single representation of zero and lets a CPU use the same adder circuit for both signed and unsigned addition/subtraction, since adding a two's complement negative number automatically produces the correct result without special-casing the sign bit.