Two's Complement Calculator: Signed Decimal and Fixed-Width Binary
Two's complement is the standard fixed-width way computers store signed integers. The width matters every time. 11111111 can mean -1 at 8 bits, but 0000000011111111 means 255 at 16 bits because the leading bit changed.
This tool converts between signed decimal and exact-width two's complement binary for 4, 8, 12, 16, 32, and 64 bits. It runs entirely in the browser, uses exact BigInt math, and hard-fails invalid input instead of silently padding, truncating, or wrapping values.
Why width is not optional
An n-bit two's complement value represents the signed range -2^(n-1) through 2^(n-1)-1.
8bits:-128to12716bits:-32768to3276732bits:-2147483648to2147483647
That range is why decimal 128 does not fit in signed 8-bit two's complement. The correct behavior is an error, not automatic wraparound.
The same rule explains why shorter binary input is rejected. If you type 1111, the calculator cannot safely guess whether you meant 4-bit -1, 8-bit 15 after left-padding, or something else entirely. Exact-width semantics prevent wrong assumptions.
How signed values are interpreted
For non-negative numbers, the binary bits work like ordinary unsigned binary with a leading 0.
For negative numbers, the most significant bit is not just a sign marker. It has negative weight. In an n-bit value, the signed value is:
-bit[n-1] * 2^(n-1) + sum(bit[i] * 2^i)
That is why 11111111 at 8 bits is -1:
- unsigned view:
255 - subtract
2^8 = 256 - result:
-1
The same pattern works for every negative two's complement value.
Decimal to two's complement
To encode a negative signed integer into n bits:
- Convert the magnitude to binary.
- Left-pad to the selected width.
- Invert every bit.
- Add
1.
Example: -42 at 8 bits
- magnitude
42->101010 - pad to 8 bits ->
00101010 - invert ->
11010101 - add
1->11010110
That final pattern is why the calculator shows:
- signed decimal:
-42 - binary:
11010110 - unsigned interpretation:
214 - hex:
0xD6
Binary to signed decimal
When the leading bit is 0, the signed and unsigned interpretations match.
When the leading bit is 1, interpret the full bit pattern as unsigned first, then subtract 2^n. For 11110000 at 8 bits:
- unsigned view:
240 - subtract
256 - result:
-16
This is the formal fixed-width definition. It is clearer than thinking of the leading bit as a separate sign flag.
The two special cases
Zero
Zero is all zeros at every width. Two's complement solved an older signed-binary problem by giving zero exactly one representation. Negating zero still produces zero.
Minimum negative value
Every width has one minimum negative value:
4bits ->1000->-88bits ->10000000->-12816bits ->1000000000000000->-32768
This value is special because negating it in the same width overflows. The positive counterpart needs one more bit than the width allows, so the same bit pattern comes back.
Why hex and unsigned views help
Signed decimal answers the math question, but developers often need the other interpretations too.
- Unsigned view helps with masks, registers, and raw storage.
- Hex is compact and matches debuggers, dumps, protocol docs, and C-style literals.
- Grouped binary keeps long values readable without changing the copied output.
Showing all three together makes it easier to debug low-level code and avoid confusing representation with arithmetic negation.
Common mistakes this tool avoids
- Silent left-padding of short binary input
- Silent wraparound for out-of-range decimal input
- Treating the leading
1as only a sign label instead of a negative-weight bit - Mixing up representation conversion with a separate "negate this bit pattern" operation
Troubleshooting
Why does 11111111 mean -1? — At 8 bits, 11111111 is unsigned 255. Two's complement signed interpretation subtracts 2^8 = 256, so 255 - 256 = -1.
Why can 10000000 mean -128? — In 8-bit two's complement, the leading bit has weight -128 and every other bit is 0, so the total is -128. It is also the minimum negative value for that width.
Why is shorter binary input rejected? — Two's complement is width-dependent. 1111 is not safely interchangeable with 00001111. Rejecting short input avoids guessing missing leading bits and changing the signed meaning.
Why does decimal 128 fail at 8 bits? — Signed 8-bit two's complement only allows values from -128 to 127. 128 needs a wider signed width.
Why does the minimum negative value warn about overflow? — Negating the minimum value in the same width would require one more positive bit than the width has, so the hardware-style result wraps back to the same bit pattern.