Two's Complement Calculator
Convert between signed decimal values and their binary representation.
What the Two's Complement Calculator does
Two’s complement represents negative numbers by inverting the bits of the magnitude and adding one. The top bit then acts as a sign flag, and ordinary binary addition gives correct results for both signs — which is why virtually every processor uses it.
Formula
Negative value = ~magnitude + 1Signed value = unsigned − 2ⁿ when the sign bit is setSigned range = −2ⁿ⁻¹ to 2ⁿ⁻¹ − 1Unsigned range = 0 to 2ⁿ − 1
Inputs explained
| Input | Unit | Required | Notes |
|---|---|---|---|
| Start from | one of 2 options | Yes | — |
| Decimal value | text | In some modes | Shown Start from is A decimal value. |
| Bit pattern | text | In some modes | Shown Start from is A binary or hex bit pattern. |
| Pattern is in | one of 2 options | In some modes | Shown Start from is A binary or hex bit pattern. |
| Bit width | one of 4 options | Yes | — |
How to use it
- Choose Start from and Bit width.
- Fill in the remaining inputs the form shows for your choice.
- Select Calculate.
Worked example
Representing −5 in 8 bits.
- Value
- −5
- Bit width
- 8-bit
5 is 00000101. Inverting gives 11111010, and adding 1 gives 11111011 — 0xFB, which reads as 251 unsigned.
Reading the result
- "Unsigned interpretation" shows the same bits read a different way — it is exactly why a signed overflow bug often surfaces as a huge positive number instead of a negative one: the bits never change, only how they are interpreted.
- For a positive value there is nothing to show under "how it was formed" — its two's complement representation is identical to its plain binary, no transformation needed.
Assumptions and limitations
- The bit width is a single, explicitly chosen value — real overflow bugs often come from an implicit width mismatch, such as assigning a wider value into a narrower variable, which this only reveals if that narrower width is deliberately entered.
Common mistakes
- Assuming "set bits" has any direct relationship to whether a value is positive or negative — a negative number can have very few set bits after the two's complement transformation and a positive one can have many, so unsigned bit-count intuition does not transfer.
Frequently asked questions
Why is the signed range asymmetric?
Zero takes one of the 2ⁿ patterns from the positive side, leaving one fewer positive value than negative. An 8-bit signed integer spans −128 to 127.
What happens on overflow?
The value wraps around. Adding 1 to 127 in 8-bit signed gives −128, because the bit pattern rolls from 01111111 to 10000000.