If you need to convert a signed decimal number to binary (and back) using fixed-width two’s complement, this Two’s Complement Calculator gives exact results. It handles negative values correctly by applying the standard two’s complement rules for a chosen bit width.
What Two’s Complement Means
Two’s complement is the most common way computers represent signed integers in binary. It uses a fixed number of bits, where the leftmost bit acts as the sign bit. Negative numbers are represented by taking the binary value of the absolute magnitude and applying a specific bit pattern transformation.
The key idea: with N bits, two’s complement represents values from -2^(N-1) to 2^(N-1)-1. That range is why choosing the bit width matters.
Core Formulas (Fixed-Width)
Decimal to two’s complement binary
To encode an integer x using N bits:
- If x >= 0, represent x in binary using exactly N bits.
- If x < 0, compute 2^N + x, then write the result in binary using N bits.
This works because adding 2^N effectively wraps the negative value into the correct unsigned bit pattern.
Two’s complement binary to decimal
To decode an N-bit two’s complement binary value:
- If the sign bit is 0, the number is positive and you interpret the bits as an ordinary unsigned binary value.
- If the sign bit is 1, the value is negative. Compute: value = unsigned_value – 2^N.
What the Bit Width Controls
The calculator requires a bit width because two’s complement depends on it. Changing from 8 bits to 16 bits changes the representable range and the exact bit pattern.
For N bits, the representable range is:
| Bit width (N) | Minimum value | Maximum value |
|---|---|---|
| 8 | -128 | 127 |
| 16 | -32,768 | 32,767 |
| 32 | -2,147,483,648 | 2,147,483,647 |
If your number falls outside the chosen range, the two’s complement result would overflow. The calculator flags this so you can adjust the bit width.
How to Use the Two’s Complement Calculator
Set the bit width, then choose one direction:
- Decimal → Binary: enter a signed decimal integer to get the exact N-bit two’s complement binary.
- Binary → Decimal: enter a binary string (with exactly N bits) to get the signed decimal value.
The calculator also shows the intermediate unsigned value used for negative numbers, which makes the conversion easy to verify.
Practical Examples
Example 1: Encode -5 as 8-bit two’s complement
Pick N = 8. The range is -128 to 127, so -5 is valid. For negative numbers, compute 2^8 + (-5) = 256 – 5 = 251. Then write 251 in 8-bit binary.
Result: -5 → 11111011. The sign bit is 1, so the value is negative, and decoding returns -5.
Example 2: Decode 11101010 as 8-bit two’s complement
Use N = 8 and treat 11101010 as two’s complement. The sign bit is 1, so the value is negative. Interpret the bits as unsigned: 11101010 = 234. Then compute 234 – 256 = -22.
Result: 11101010 → -22.
Common Mistakes to Avoid
- Wrong bit width: two’s complement is fixed-width. A binary string that is correct for 8 bits may represent a different value for 16 bits.
- Missing leading bits: you must include all N bits when decoding.
- Assuming sign-magnitude: two’s complement uses a different negative encoding method than sign-magnitude.
- Ignoring overflow: if the decimal value is outside the range, the computed pattern won’t match the intended integer.
Frequently Asked Questions
What is a two’s complement calculator used for?
A Two’s Complement Calculator converts between signed decimal integers and fixed-width two’s complement binary. Use it when you need to verify how negative numbers are stored in hardware, debug assembly or machine code, or answer questions in digital logic and computer architecture.
How do I choose the right bit width?
Choose the bit width that matches the system you’re modeling, such as 8-bit bytes or 16-bit integers. The representable range is -2^(N-1) to 2^(N-1)-1. If your number is outside that range, the value overflows.
Why does two’s complement use 2^N + x for negatives?
For a negative integer x, two’s complement forms an equivalent unsigned pattern by adding 2^N. That wrap-around ensures the binary bits behave correctly under addition and subtraction. When decoding, subtracting 2^N restores the original negative value.
Can I convert any binary string to a signed number?
Yes, as long as you specify the bit width N and the binary string has exactly N bits. If the string has the wrong length or includes characters other than 0 and 1, the result is undefined. The sign bit determines whether to subtract 2^N.
Does two’s complement make arithmetic easier?
Yes. Two’s complement enables the same binary add circuitry to handle both positive and negative numbers. Overflow rules still apply, but addition and subtraction can be performed with consistent hardware steps. That’s why it is the standard representation in most CPUs.
Next Steps
Use the calculator to check your work for class assignments, interview problems, or debugging sessions. If you’re learning, try converting a sequence like -1, -2, -3 and compare how the bit patterns change as the sign bit stays at 1.



