Two's Complement: Negative Numbers in Binary
Two's complement is how a fixed-width binary value carries a sign, and it is why 11111011 can be 251 or −5 depending on who reads it.
| Bits | Width | Read as unsigned | Read as two's complement |
|---|---|---|---|
| 11111011 | 8-bit | 251 | -5 |
What is two's complement?
Two's complement is the standard way to write negative binary numbers at a fixed width: -5 in 8 bits is 11111011. Write +5 (00000101), invert every bit (11111010), and add 1.
The width comes first, because the sign lives in the top place. In eight digits the leading place is worth −128 instead of +128 and every other place keeps its usual weight, so 11111011 adds up to −128 + 64 + 32 + 16 + 8 + 2 + 1.
Result: 11111011 is 251 unsigned and −5 signed
11111111 is 255 as an unsigned 8-bit number (0xFF, 0o377). Read as 8-bit two's complement it is −1; the 8-bit signed range is −128 to 127.
| Bits | Unsigned | Signed | Oct | Hex |
|---|---|---|---|---|
| 00000000 | 0 | 0 | 000 | 00 |
| 00000001 | 1 | 1 | 001 | 01 |
| 01111111 | 127 | 127 | 177 | 7F |
| 10000000 | 128 | -128 | 200 | 80 |
| 11111011 | 251 | -5 | 373 | FB |
| 11111111 | 255 | -1 | 377 | FF |
Nothing in the digits says which column applies. A value is signed or unsigned because the program reading it says so, which is why one byte can be 251 in one place and −5 in another.
How do you convert a negative decimal to two's complement binary?
Write the value without its sign, invert every digit, then add 1. The width has to be fixed before you start.
- Fix the width. Eight digits hold −128 to 127, so −5 fits with room to spare.
- Write the value without its sign in binary, padded to that width: 5 is 00000101.
- Invert every digit, one for zero and zero for one: 00000101 becomes 11111010.
- Add 1 to the inverted value: 11111010 plus 1 is 11111011.
Result: −5 = 11111011 in 8 digits
The same four steps work at any width; only the padding changes. A positive value needs none of them, so write a number in base 2 gives the same digits.
How do you convert two's complement binary to decimal?
Read the leading digit first. A 0 means the value is positive and reads as an ordinary binary number; a 1 means it is negative, and the steps below recover its size.
- Look at the leading digit. 11111011 starts with 1, so the value is negative.
- Invert every digit: 11111011 becomes 00000100.
- Add 1: 00000100 plus 1 is 00000101, which is 5.
- Put the sign back: the original eight digits are −5.
Result: 11111011 = −5 in 8 digits
The place-value route reaches the same answer in one line. Keep every weight as it is and make the top one negative: −128 + 64 + 32 + 16 + 8 + 2 + 1 is −5. For unsigned values of any width, binary to decimal with the power-of-2 row shows the working place by place.
What is the difference between sign-magnitude, one's complement and two's complement?
Sign-magnitude and one's complement both leave two ways of writing zero; two's complement leaves one, which is why hardware settled on it.
| Scheme | Minus 5 in 8 bits | Ways to write zero | Range |
|---|---|---|---|
| Sign-magnitude | 10000101 | Two | −127 to 127 |
| One's complement | 11111010 | Two | −127 to 127 |
| Two's complement | 11111011 | One | −128 to 127 |
The second reason is arithmetic. In two's complement, adding a negative value is the same operation as adding a positive one, so a processor needs one adder rather than an adder and a special case. The other two schemes need a correction step whenever a carry leaves the top place.
What is the range of an 8-bit two's complement number?
An 8-bit two's complement value runs from −128 to 127: 128 negative values, zero, and 127 positive ones. The negative side reaches one further because zero sits on the positive side of the split.
A byte is 8 bits and holds 256 values, 0–255; a nibble is 4 bits.
A nibble's 16 values run 0–15 unsigned and −8 to 7 signed. Doubling the width squares the count: 16 digits give 65536 patterns, split into 2¹⁵ = 32768 negatives and 32768 values from zero up. The same split holds at every width, so n digits run from −2ⁿ⁻¹ to 2ⁿ⁻¹ − 1, which is why the page on number system conversion treats width and base as two separate questions.
| Width | Two's complement | Unsigned |
|---|---|---|
| 4 bits | −8 to 7 | 0–15 |
| 8 bits | −128 to 127 | 0–255 |
| 16 bits | −32768 to 32767 | 0–65535 |
| 32 bits | −2147483648 to 2147483647 | 0–4294967295 |
| 64 bits | −9223372036854775808 to 9223372036854775807 | 0–18446744073709551615 |
Frequently Asked Questions
What is binary overflow?
Overflow is a result that needs more digits than the width allows. In 8-bit two's complement, 127 plus 1 gives 10000000, which reads as −128: the value wraps to the other end of the range instead of growing.
The rest of the family sits on all the number and text converters in one place.