Two's Complement Calculator
Enter a signed number, a binary pattern or a hex value, pick a width from 4 to 64 bits, and see the two's complement bits, the signed and unsigned readings, the legal range and the invert-and-add-one working. A value that does not fit is flagged and shown wrapped, the way hardware would store it. Free, no sign-up, and everything runs in your browser.
A whole number, positive or negative, of any size. Spaces and underscores are ignored.
Result
1101 0101
Two's complement bits
D5
Hexadecimal
−43
Signed value
213
Same bits read as unsigned
−128 to 127
Range at this width
7 bits
Smallest width that fits
How the bits were found
| Step | Bits |
|---|---|
| Write the size of the number (without its sign) in 8 bits | 0010 1011 |
| Flip every bit (this is the one's complement) | 1101 0100 |
| Add 1 — the result is the two's complement | 1101 0101 |
Shortcut: 2⁸ − 43 = 213, where 2⁸ is 256.
Place values
The sign bit counts as −2⁷; every other 1 bit adds its power of two.
−128 + 64 + 16 + 4 + 1 = −43
The same value in three signed formats
| Format | Bits |
|---|---|
| Sign and magnitude | 1010 1011 |
| One's complement | 1101 0100 |
| Two's complement | 1101 0101 |
Sign extension
To keep the same value in 16 bits, copy the sign bit into the new top bits: 1111 1111 1101 0101
How to Use the Two's Complement Calculator
- 1. Say what you are entering. A signed decimal number (to get the bits), or a binary or hexadecimal pattern (to get the value those bits mean).
- 2. Set the width. Type any whole number from 4 to 64, or use a preset. The same value has a different pattern at 8 bits than at 16.
- 3. Read the result. You get the bits, the hex form, the signed value, the same bits read as unsigned, the range for that width and the smallest width that holds the value.
- 4. Follow the working. The tables below the result show the invert-and-add-one steps, the place values and the other signed formats.
What Two's Complement Is
Two's complement is the way nearly every processor stores signed integers. In an N-bit pattern, the top bit carries a negative weight of −2N−1 and every other bit carries its ordinary positive power of two. The value is therefore −s · 2N−1 + Σ bᵢ · 2ⁱ, where s is the sign bit. Its great advantage is that the same adder circuit handles signed and unsigned numbers, because subtraction becomes addition of a negated value.
| Width | Smallest value | Largest value |
|---|---|---|
| 4 bits | −8 | 7 |
| 8 bits | −128 | 127 |
| 16 bits | −32,768 | 32,767 |
| 32 bits | −2,147,483,648 | 2,147,483,647 |
| 64 bits | −9,223,372,036,854,775,808 | 9,223,372,036,854,775,807 |
There is one more negative value than positive, because zero uses up one of the patterns whose sign bit is 0. That asymmetry is where most two's-complement surprises come from.
Worked Example: −43 in 8 Bits
- Write 43 in 8 bits:
00101011. - Flip every bit (the one's complement):
11010100. - Add 1:
11010101. That isD5in hex, and 213 if you read it as unsigned. - Check with place values: −128 + 64 + 16 + 4 + 1 = −43.
There is a shortcut for the same answer: the pattern is 2⁸ − 43 = 256 − 43 = 213, which is 11010101. To go back the other way, run the same two steps on the pattern — flip and add 1 — and you recover the size, 43, with the sign bit telling you it was negative. Negating twice returns the original pattern.
Overflow and Wrap-Around
A number outside the range cannot be stored. Hardware does not complain; it keeps the low N bits and discards the rest. At 8 bits, 127 + 1 produces 10000000, which reads as −128, and 128 itself wraps to −128. In the other direction −129 wraps to 127. The calculator flags any value that does not fit, shows the wrapped result and tells you the smallest width that would hold it.
The most negative value has no positive twin
Negating −128 at 8 bits gives −128 again: flipping 10000000 gives 01111111, and adding 1 returns 10000000. This is why abs() of the minimum integer is a classic bug in C and Java.
Two's Complement Compared with Other Signed Formats
| Format | −5 | +5 | Zero |
|---|---|---|---|
| Sign and magnitude | 10000101 | 00000101 | Two zeros: 00000000 and 10000000 |
| One's complement | 11111010 | 00000101 | Two zeros: 00000000 and 11111111 |
| Two's complement | 11111011 | 00000101 | One zero: 00000000 |
Sign-magnitude and one's complement both have a "negative zero" and need extra logic in the adder, which is why two's complement won. The widget's last table shows your own value in all three formats, and marks the minimum value as having no pattern in the other two.
Sign Extension
To widen a signed number without changing its value, copy the sign bit into the new top bits: 11010101 (−43) becomes 1111111111010101 at 16 bits. Padding with zeros instead would turn it into 213. Compilers do this for you when a narrow signed type is promoted, and forgetting it is a common cause of bad values when reading binary file formats.
Frequently Asked Questions
- How do you find the two's complement of a binary number?
- Flip every bit, then add 1. For 00101011 (43), flipping gives 11010100 and adding 1 gives 11010101, which is −43 in 8 bits. The result is the pattern for the negative of the original number.
- How do you convert two's complement back to decimal?
- If the top bit is 0, read the pattern as ordinary binary. If it is 1, flip all bits, add 1, read the result as binary and put a minus sign in front. Or add up the place values with the top bit counted as negative: for 11010101 that is −128 + 64 + 16 + 4 + 1 = −43.
- What is the range of an 8-bit two's complement number?
- From −128 to 127. In general an N-bit two's complement integer runs from −2N−1 to 2N−1 − 1, which is 2N distinct values in total. The widget shows the range for any width from 4 to 64.
- What happens when a two's complement number overflows?
- The extra high bits are dropped and the remaining N bits are read as a signed number, so the value wraps around. In 8 bits, 127 + 1 becomes −128. Signed overflow is undefined behaviour in C and C++, but it is defined to wrap in Java and in Rust's wrapping operations.
- What is the difference between one's complement and two's complement?
- One's complement just flips the bits of the positive number, which leaves two representations of zero. Two's complement flips the bits and adds 1, which gives a single zero and lets ordinary binary addition work for negative numbers. Two's complement is what modern CPUs use.
- Why is there one more negative number than positive?
- Half of the 2N patterns have a 1 in the sign bit, so there are 2N−1 negative values (−1 down to −2N−1). The other half have a 0 in the sign bit, and one of those patterns is zero, leaving only 2N−1 − 1 positive values. Zero takes the place a positive number would have had.