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
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.