Two's Complement Calculator
Test cases replayed : 4 of 4 match · Methodology · No outside source listed
Enter a signed whole number and a bit width to see how a computer stores it in two's complement: the bit pattern and the same bits read as an unsigned number.
Result
Two's complement bit pattern (leading zeros not shown)
Smallest value for this width −2ⁿ⁻¹
- Largest value for this width 2ⁿ⁻¹ − 1
- Same bits read as unsigned (x, or 2ⁿ + x when x is negative)
Estimate for planning only, not tax, legal, financial or medical advice. This calculator uses the method explained on this page.
This browser did not allow copying. The sentence is in the box below, already selected: copy it with your keyboard or the menu.
How to use this calculator
Fill in the fields in this order; each step takes you to its field.
- Signed whole number (decimal): enter a number.
- Bit width: choose 4 bits, 8 bits, 12 bits or 16 bits.
- Read the result: Two's complement bit pattern (leading zeros not shown); the panel also shows Smallest value for this width −2ⁿ⁻¹, Largest value for this width 2ⁿ⁻¹ − 1 and Same bits read as unsigned (x, or 2ⁿ + x when x is negative).
How this calculator works
In n-bit two's complement, a non-negative number is written in plain binary and a negative number x is stored as the binary form of 2ⁿ + x. The leftmost bit is then 1 for every negative number, and the representable range is −2ⁿ⁻¹ to 2ⁿ⁻¹ − 1 (−128 to 127 for 8 bits).
The same pattern is obtained by hand by writing |x| in binary, flipping every bit (the ones' complement) and adding 1. A number outside the range for the chosen width cannot be stored and is shown as a dash; for positive numbers, the bit pattern omits the leading zeros up to the chosen width.
Worked example
| Signed whole number (decimal) | -5 |
|---|---|
| Bit width | 8 bits |
| Smallest value for this width −2ⁿ⁻¹ | -128 |
|---|---|
| Largest value for this width 2ⁿ⁻¹ − 1 | 127 |
| Same bits read as unsigned (x, or 2ⁿ + x when x is negative) | 251 |
| Two's complement bit pattern (leading zeros not shown) | 11111011 |
Frequently asked questions
What is −5 in 8-bit two's complement?
11111011, which is 251 read as an unsigned byte (256 − 5).
What is −1 in 16-bit two's complement?
Sixteen ones, 1111111111111111, or 65,535 read as unsigned.
Can 200 be stored in 8-bit two's complement?
No. The 8-bit range is −128 to 127, so 200 needs at least 9 bits.