How the two's complement calculator works
Choose a width and an encoding, then type a decimal number. The result card shows the bit pattern in groups of four with its hex form, and the bit grid marks the sign bit. For a negative number in two's complement, the working shows the size in binary, every bit flipped, and the 1 added, with a check that reading the pattern as unsigned and subtracting 2 to the power of the width gives your number back.
The βread four waysβ table shows what the same bits mean as unsigned, two's complement, one's complement and sign-magnitude β a pattern never records its own encoding. To add or subtract signed values, use the binary calculator.
The two's complement formula
Example: β5 in 8 bits is 2βΈ β 5 = 251 = 11111011. Reading it back: β128 + 64 + 32 + 16 + 8 + 2 + 1 = β5.
Worked examples
Example 1 β encoding β100 in 8 bits
- Number:
- β100
- Width:
- 8 bits
- 100 in binary: 01100100
- Invert every bit: 10011011
- Add 1: 10011100
Two's complement: 10011100 (0x9C)
Check: 10011100 is 156 unsigned, and 156 β 256 = β100.
Example 2 β decoding a 16-bit pattern
- Pattern:
- 1111 1111 1000 0000
- Top bit is 1: negative
- Read as unsigned: 65,408
- Subtract 2ΒΉβΆ: 65,408 β 65,536
Value: β128
It is the 8-bit pattern 10000000 sign-extended to 16 bits by copying the 1 into the new top byte.
Integer ranges by width
| Width | Two's complement (signed) | Unsigned |
|---|---|---|
| 8-bit | β128 to 127 | 0 to 255 |
| 16-bit | β32,768 to 32,767 | 0 to 65,535 |
| 32-bit | β2,147,483,648 to 2,147,483,647 | 0 to 4,294,967,295 |
| 64-bit | β9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 | 0 to 18,446,744,073,709,551,615 |
Key facts
- In n-bit two's complement, a negative number βx is stored as the unsigned value 2βΏ β x.
- An 8-bit two's complement integer holds β128 to 127, and a 32-bit one holds β2,147,483,648 to 2,147,483,647.
- In two's complement, β1 is all ones at every width, and the most negative number is a 1 followed by all zeros.
- Since C++20, two's complement is the only signed integer representation the C++ standard allows.
Why computers use two's complement
With two's complement, the circuit that adds unsigned numbers also adds signed ones: 11111011 (β5) plus 00000011 (3) is 11111110, which is β2, with no special case for the sign. There is also only one zero, unlike one's complement and sign-magnitude. That is why every mainstream processor uses it, and why C++20 made it the only representation the language allows (source: cppreference). You need this when reading a memory or register dump, decoding a sensor value that arrives as raw bytes, debugging an integer overflow, or answering a computer architecture exam question.
Common two's complement mistakes
- Forgetting the width. β5 is 11111011 in 8 bits but 1111111111111011 in 16. Without a width a negative pattern is meaningless.
- Inverting without adding 1. That gives one's complement: 11111010 is β5 only in one's complement; read as two's complement it is β6.
- Padding with zeros. Widening a negative value means copying its sign bit (sign extension), not adding zeros on the left.
- Negating the minimum. β(β128) does not fit in 8 bits; inverting and adding 1 to 10000000 gives 10000000 again.
Frequently Asked Questions
What is β1 in two's complement?
β1 is all ones at every width: 11111111 in 8 bits, 16 ones in 16 bits, and so on. Invert 00000001 to get 11111110, then add 1 to get 11111111. Read as unsigned, the same pattern is 255.
How do you find the two's complement of a negative number?
Write the number's size in binary at the full width, flip every bit, then add 1. For β12 in 8 bits: 12 is 00001100, flipped it is 11110011, and adding 1 gives 11110100. Another way is to subtract the size from 2 to the power of the width: 256 β 12 = 244, which is 11110100.
How do you convert a two's complement number back to decimal?
If the top bit is 0, read it as ordinary binary. If the top bit is 1, the number is negative: read the bits as unsigned and subtract 2 to the power of the width. 11110100 is 244 unsigned, and 244 β 256 = β12.
Why can 8 bits hold β128 but only +127?
There are 256 patterns. Zero uses one, leaving 255 to split between negatives and positives, so one side gets an extra value. Two's complement gives it to the negatives: 10000000 is β128, and its positive counterpart +128 would need a ninth bit.
What happens when a two's complement number overflows?
Adding 1 to the largest value wraps to the smallest: in 8 bits, 127 + 1 gives 10000000, which reads as β128. The calculator reports a value outside the range as an overflow, shows the range, and shows the pattern a register would wrap it to.
What is the difference between one's and two's complement?
One's complement only flips the bits, so β5 is 11111010 and there are two zeros, 00000000 and 11111111. Two's complement flips the bits and adds 1, so β5 is 11111011, there is one zero, and ordinary binary addition gives the right answer for negative numbers too.
Related calculators
Notes & Assumptions
- Inputs are whole numbers, worked exactly with arbitrary-precision integers at 8, 16, 32 or 64 bits.
- A value outside the range is reported as overflow rather than wrapped silently; for unsigned and two's complement the wrapped pattern is shown as well.
- Fractional and floating-point values are not encoded here; the IEEE 754 converter covers those.
This tool is for education and general calculation. Check how a particular language or processor handles overflow against its documentation.