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.

What Is Two's Complement?

Two's complement is the scheme computers use to represent signed integers — numbers that can be positive or negative — as plain bit patterns. Positive numbers are stored as ordinary binary, while a negative number is formed by flipping every bit of its absolute value and then adding one. The most significant bit (MSB) doubles as the sign: 1 means negative, 0 means positive, and the range that can be represented depends on the chosen bit width (8, 16, 32, or 64 bits).

This tool converts a signed decimal integer into its two's complement binary pattern at a bit width you choose, and can also convert the other way, from a binary string back to decimal. Alongside the main result it shows the unsigned interpretation of the same bits, the hexadecimal notation, and a side-by-side sign-magnitude comparison, so you can see concretely how a single bit pattern can mean different things depending on how it is read.

How to Use the Two's Complement Calculator

  1. Choose a bit width Pick 8, 16, 32, or 64 bits to match the integer type you want to check.
  2. Pick a conversion direction Switch between "decimal → binary" and "binary → decimal" depending on which way you need to convert.
  3. Enter a value Type a signed decimal integer (e.g. -5) or a binary bit string (e.g. 11111011).
  4. Read the converted result The signed and unsigned decimal values, the two's complement bit pattern, and the hex notation all appear together.
  5. Check the range/overflow warning Enter a value outside what the bit width can hold to see exactly where the overflow boundary lies.

Tips for getting more out of it

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

Ways to Use Two's Complement Conversion

Studying for computer science courses and exams

Work through the binary conversion problems that show up in nearly every intro CS course, and check your answers by hand.

Debugging integer overflow bugs in C or embedded code

Reproduce an unexpected value from an int or uint8_t variable at its real bit width to track down exactly where it went wrong.

Understanding how CPU registers store values

When a debugger shows a register's hex value, quickly check what signed integer that pattern actually represents.

Verifying bitwise operation results

Before and after writing AND/OR/shift logic, confirm by hand that the resulting bit pattern matches what you expected.

Two's Complement Glossary

Two's Complement
A way of representing negative numbers by flipping every bit of the absolute value and adding one. It gives zero a single unambiguous pattern and lets the same adder circuit handle both addition and subtraction, which is why nearly every modern CPU uses it for signed integers.
Sign Bit
The most significant bit (MSB) of a bit pattern. In two's complement, a value of 1 means the number is negative and 0 means it is positive.
Overflow
What happens when a result no longer fits within the available bit width. The extra high-order bit is discarded, which changes the meaning of the sign bit and makes the value appear to wrap around to the opposite end of the range.
Signed vs. Unsigned Integer
The same bit pattern can represent two different values depending on whether the sign bit is treated as a marker for negative numbers (signed) or simply as another value bit (unsigned).
Bit Width
The number of bits used to store an integer. Common widths are 8, 16, 32, and 64 bits — the wider the type, the larger the range of values it can represent.
One's Complement
An older scheme that forms a negative number by simply flipping every bit of the positive value, with no added 1. It has the drawback of producing two distinct patterns for zero ("+0" and "-0"), a problem two's complement was designed to eliminate.

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

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.