Two’s complement calculator: decimal, binary and hex
Convert a signed decimal number to two’s complement binary and hex, or decode a bit pattern back to decimal, at 4, 8, 16, 32 or 64 bits. The calculator shows the invert-and-add-one working, compares sign-magnitude and ones’ complement, and adds two numbers with the carries and overflow check drawn out.
On this page
What two’s complement is
Computers store integers in a fixed number of bits. With bits there are patterns, and an unsigned integer uses them for 0 to . To represent negative numbers too, almost every processor built since the 1970s uses two’s complement: the patterns are the same, but the top bit gets a negative weight.
In 8 bits the weights are −128, 64, 32, 16, 8, 4, 2, 1. So 01111111 is 127, the largest value, and 10000000 is −128, the smallest. 11111111 is −128 + 127 = −1. A leading 1 always means the number is negative, which is why the top bit is called the sign bit, but unlike sign-magnitude the other bits are not simply the size of the number.
The range of an -bit two’s complement integer is
| Width | Typical type | Minimum | Maximum |
|---|---|---|---|
| 8 bits | int8_t, byte (Java) | −128 | 127 |
| 16 bits | int16_t, short | −32,768 | 32,767 |
| 32 bits | int32_t, int | −2,147,483,648 | 2,147,483,647 |
| 64 bits | int64_t, long | −9,223,372,036,854,775,808 | 9,223,372,036,854,775,807 |
Converting a negative number: invert and add one
Worked example: −18 in 8 bits
- Write the magnitude, 18, in binary and pad to 8 bits:
0001 0010. - Invert every bit (this is the ones’ complement):
1110 1101. - Add 1:
1110 1110. That is −18, or0xEEin hex. - Check with the weights: −128 + 64 + 32 + 8 + 2 = −18. ✓
Why does this work? Inverting in bits gives , so adding one gives . That is the second method: a negative number is stored as the unsigned number . For −18 in 8 bits, , and 238 in binary is 1110 1110. Negation works the same way in both directions: invert and add one to 1110 1110 and you get 18 back.
A quick shortcut by hand: copy bits from the right up to and including the first 1, then invert everything to its left. For 18 = 0001 0010, keep 10, flip the rest to get 1110 1110.
Converting two’s complement to decimal
If the top bit is 0, read the number as ordinary binary. If it is 1, either add up the weights with the top one negative, or read the pattern as unsigned and subtract . For 1010 0110: unsigned it is 166, and . Or invert and add one to get the magnitude: 0101 1010 = 90, so the value is −90. Choose “Binary or hex → decimal” above to see each step for your own pattern. Hex works too: 0xFF in 8 bits is −1, and 0x8000 in 16 bits is −32,768.
Changing width keeps the value only if you sign-extend: copy the sign bit into the new high bits. −18 in 16 bits is 1111 1111 1110 1110, not 0000 0000 1110 1110 (which is 238).
Why computers use two’s complement
There are three classic ways to store signed integers. Here are all of them in 4 bits:
| Value | Two’s complement | Ones’ complement | Sign-magnitude |
|---|---|---|---|
| −8 | 1000 | — | — |
| −7 | 1001 | 1000 | 1111 |
| −6 | 1010 | 1001 | 1110 |
| −5 | 1011 | 1010 | 1101 |
| −4 | 1100 | 1011 | 1100 |
| −3 | 1101 | 1100 | 1011 |
| −2 | 1110 | 1101 | 1010 |
| −1 | 1111 | 1110 | 1001 |
| 0 | 0000 | 0000 | 0000 |
| 1 | 0001 | 0001 | 0001 |
| 2 | 0010 | 0010 | 0010 |
| 3 | 0011 | 0011 | 0011 |
| 4 | 0100 | 0100 | 0100 |
| 5 | 0101 | 0101 | 0101 |
| 6 | 0110 | 0110 | 0110 |
| 7 | 0111 | 0111 | 0111 |
Sign-magnitude and ones’ complement both waste a pattern on a second zero (1000 and 1111 respectively are “−0”), and both need special cases in the adder. Two’s complement has these advantages:
- One zero, so it represents one extra negative number: −8 in 4 bits, −128 in 8.
- The same adder works for signed and unsigned numbers. Addition is just binary addition modulo , so the hardware doesn’t need to know which kind it is adding.
- Subtraction is addition: , so a subtractor is an adder with inverters and a carry-in of 1.
- Comparison with zero just looks at the top bit, and multiplication’s low half is the same as for unsigned numbers.
The one odd value is the minimum. −128 has no positive partner in 8 bits, so negating it gives −128 again. That is why abs(INT_MIN) overflows in C and Java.
Addition and overflow
Add two’s complement numbers exactly as unsigned binary and keep the low bits. For example, −7 + 5 in 8 bits gives 1111 1110 = −2, correct with no special handling. The result is wrong only when the true sum is outside the range, which is called overflow:
100 + 50 in 8 bits gives 1001 0110, which reads as −106: two positive numbers produced a negative result, because the true sum 150 is above 127. Try it in “Add two numbers”. In hardware, overflow is the XOR of the carry into the sign bit and the carry out of it. In C and C++ signed overflow is undefined behaviour, so compilers may assume it never happens; Java and C# wrap round silently, and Rust panics in debug builds.
Related
- Non-integers use a different format: see the IEEE 754 floating-point converter.
- The logic behind an adder circuit is on the logic gate simulator and Boolean algebra pages.
References
- R. E. Bryant and D. R. O’Hallaron, Computer Systems: A Programmer’s Perspective, 3rd ed., Pearson, 2016. Sections 2.2–2.3.
- D. A. Patterson and J. L. Hennessy, Computer Organization and Design: The Hardware/Software Interface, RISC-V ed., Morgan Kaufmann, 2017. Section 2.4.
- D. E. Knuth, The Art of Computer Programming, Volume 2: Seminumerical Algorithms, 3rd ed., Addison-Wesley, 1997. Section 4.1.