Two’s Complement Explained With 4- and 8-Bit Examples
How two’s complement stores negative numbers: invert and add one, read the top bit as negative, and see signed ranges, sign extension and overflow live.
Two’s complement is how computers store signed integers. The leftmost bit has a negative weight, so in 8 bits it is worth −128 instead of +128. To negate a number, invert every bit and add 1: 5 is 00000101, so −5 is 11111010 + 1 = 11111011.
128 + 64 + 32 + 16 + 8 + 2 + 1
Unsigned Signed (two’s complement) Hex
Read a two’s complement number
Use the usual place values, but make the top one negative. For 8 bits the weights are −128, 64, 32, 16, 8, 4, 2, 1.
| Bits | Calculation | Value |
|---|---|---|
| 0000 0101 | 4 + 1 | 5 |
| 1111 1011 | −128 + 64 + 32 + 16 + 8 + 2 + 1 | −5 |
| 1000 0000 | −128 | −128 |
| 0111 1111 | 64 + 32 + 16 + 8 + 4 + 2 + 1 | 127 |
| 1111 1111 | −128 + 127 | −1 |
A leading 1 always means negative, a leading 0 always means zero or positive. That is why the top bit is often called the sign bit, even though it is a weighted bit like the others rather than a separate plus/minus flag.
Negate: invert and add one
- Write the positive number in the full width: 6 in 4 bits is
0110. - Invert every bit (this is the one’s complement):
1001. - Add 1:
1010. Check: −8 + 2 = −6.
The same steps turn a negative number back into a positive one. A shortcut that avoids the addition: copy bits from the right up to and including the first 1, then invert everything to its left. For 0110, keep 10, invert 01 to 10, giving 1010.
4-bit table
With four bits, all 16 patterns map to −8 … 7:
| Bits | Unsigned | Signed | Bits | Unsigned | Signed |
|---|---|---|---|---|---|
| 0000 | 0 | 0 | 1000 | 8 | −8 |
| 0001 | 1 | 1 | 1001 | 9 | −7 |
| 0010 | 2 | 2 | 1010 | 10 | −6 |
| 0011 | 3 | 3 | 1011 | 11 | −5 |
| 0100 | 4 | 4 | 1100 | 12 | −4 |
| 0101 | 5 | 5 | 1101 | 13 | −3 |
| 0110 | 6 | 6 | 1110 | 14 | −2 |
| 0111 | 7 | 7 | 1111 | 15 | −1 |
Notice the rule in the right half: signed value = unsigned value − 16. In general, for an n-bit pattern with the top bit set, signed = unsigned − 2ⁿ. That is the fastest way to convert by hand: 11111011 is 251 unsigned, and 251 − 256 = −5.
Signed ranges
An n-bit two’s complement integer holds −2ⁿ⁻¹ to 2ⁿ⁻¹ − 1:
| Width | Minimum | Maximum |
|---|---|---|
| 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 takes one of the non-negative patterns. The most negative number has no positive partner: negating −128 in 8 bits gives 10000000 back again.
Sign extension
To widen a signed value, copy the sign bit into the new high bits. −5 in 8 bits is 11111011; in 16 bits it is 11111111 11111011. Padding with zeros instead would give 251, a different number. Use the width selector in the widget above to see this happen.
Why computers use it
Two’s complement lets the same adder circuit handle signed and unsigned numbers. Adding 11111011 (−5) and 00000111 (7) gives 1 00000010; drop the carry out of bit 7 and you have 00000010 = 2, the right answer, with no special case for the sign. There is also only one zero, unlike one’s complement and sign-magnitude, which both have +0 and −0.
Every mainstream processor uses it, and both C++20 and C23 now require two’s complement for signed integers. In Python, integers are unbounded, but the bitwise operators behave as if numbers had infinitely many sign bits: ~5 is −6, because ~x is defined as −(x + 1).
When the result does not fit
Adding 1 to 127 in 8-bit signed arithmetic produces 10000000, which reads as −128. That wraparound is overflow. Try it with the +1 button above, then read the integer overflow guide for what different languages do about it. To convert any width of signed binary, use the signed binary converter.
Sources
- Fundamental types (integer representation) — cppreference.com. C++20 requires two’s complement for signed integers
- Arithmetic types — cppreference.com. C23 requires two’s complement
- Binary bitwise operations — Python Language Reference. ~x is defined as −(x+1)
How we made this
Computed and checked by tests. Ranges, negations and sign-extension examples are produced by src/lib/calc/bits.js and asserted in tests/calc.test.mjs (for example toBits(−5, 8) = 11111011 and signedOf(10000000) = −128).
Performed by Claude, an AI assistant working in the BinConvert repository (Node.js 25 test runner), November 16, 2026.
This article was drafted with AI assistance. Every example, calculation and command output on this page was re-run or checked before release; each bit pattern in the tables was recomputed by the test suite. Review: automated test suite and source check, November 16, 2026.
Spotted an error? Tell us and we will correct it.