Two's Complement Calculator (Signed Integer Binary Converter)

Convert a signed decimal integer to its 8/16/32/64-bit two's complement binary pattern, or convert back the other way. Also shows the unsigned interpretation, hexadecimal notation, and detects out-of-range values.

Tips

  • An 8-bit signed integer ranges from -128 to 127. Try entering 127 and then 128 to see the overflow boundary in action — 128 doesn't fit and triggers the range error, which is exactly where the wraparound to -128 would occur.
  • The "unsigned decimal" row reads the same bit pattern without treating the top bit as a sign. Comparing it with the signed row makes the difference between signed and unsigned interpretation immediately visible.
  • A binary input must have exactly as many digits as the selected bit width — 8 digits for 8-bit, 16 for 16-bit, and so on.
  • 64-bit calculations use BigInt internally rather than JavaScript's Number type, so values near 2^63 convert without any precision loss.
  • This tool is also handy for understanding integer overflow behavior in languages like C and Java, where int (32-bit) and long (64-bit) types wrap around at these same boundaries.

Frequently Asked Questions

Sign-magnitude and one's complement representations both end up with two distinct bit patterns for zero ("+0" and "-0"), which complicates comparison and arithmetic circuits. Two's complement has exactly one bit pattern for zero, and — just as importantly — the same adder circuit that performs addition can also perform subtraction. Both properties let CPU designers simplify the arithmetic logic unit, which is why nearly every modern processor uses two's complement internally for signed integers.

-128 to 127. Because the top bit is reserved for the sign, only 128 distinct positive-side patterns exist (0 through 127), but the negative side can represent 128 distinct values (-1 through -128) since two's complement has just one bit pattern for zero, leaving one extra pattern available on the negative side — hence the asymmetric range.

One's complement forms a negative number by simply inverting every bit of the positive value. Two's complement goes one step further and adds 1 after inverting. That extra step eliminates the "two zeros" problem inherent to one's complement (00000000 for "+0" and 11111111 for "-0"), which is why two's complement became the standard.

Because the bit width is fixed, any arithmetic result that exceeds the representable range gets its high-order bit truncated, which changes what the sign bit means. For example, adding 1 to the 8-bit signed maximum of 127 produces the bit pattern 10000000, which is interpreted as -128 under signed rules. Exceeding the upper bound wraps you around to the lower bound — that counterintuitive jump is exactly what overflow is.

The bit pattern itself doesn't change, but without treating the top bit as a sign, every bit simply contributes its normal positional value. For example, the 8-bit pattern 11111011 equals -5 when read as signed, but 251 when read as unsigned. This exact reinterpretation happens in practice whenever you use an "unsigned" type, like C's unsigned int, on a value that was previously treated as signed.
ツールくん

Side Note — Why computers use two's complement

Two's complement isn't the only way to represent negative numbers in binary. Sign-magnitude representation (using the top bit purely as a sign flag) and one's complement (simply inverting the bits of a positive number) were both used historically. Both share a common flaw: they end up with two distinct bit patterns for zero, "+0" and "-0", which complicates comparison logic and circuit design. Two's complement avoids this "two zeros" problem entirely — every bit pattern maps to exactly one integer value.

An even bigger advantage of two's complement is that the same adder circuit used for addition can also perform subtraction, since subtracting a number is equivalent to adding its two's complement. The CPU doesn't need to know or care whether a value is positive or negative; it just adds bit patterns together and gets the correct result either way. This lets the arithmetic logic unit (ALU) skip a dedicated subtraction circuit entirely, which is why virtually every modern CPU architecture settled on two's complement for signed integers.

Integer overflow is a direct consequence of this representation. Adding 1 to the 8-bit signed maximum, 127 (01111111), simply carries the bit pattern over to 10000000 — which, read as a signed value, is -128. In C, signed integer overflow is technically undefined behavior, meaning compiler optimizations can produce surprising results around this boundary, so it deserves real caution in production code. Java, by contrast, specifies that overflow silently wraps around, which is an interesting example of how much behavioral guarantees can differ between languages even when the underlying bit representation is identical.