Two's Complement
calculator.
Store a negative whole number as signed binary, or read a bit pattern back to decimal.
- Decimal
- −42
- One's complement
- 1101 0101
- Sign and magnitude
- 1010 1010
−42 is negative, so its leftmost bit is 1 and counts as −128. Read as an unsigned number, the same bits mean 214.
The bits
Click a bit to flip it8-bit pattern 1101 0110. The sign bit, marked "sign", weighs −128; the others weigh their usual powers of 2. The 1 bits add up to −42.
Working
- 1Write 42 in binary at 8 bits0010 1010
- 2Invert every bit: each 0 becomes 1 and each 1 becomes 01101 0101
- 3Add 11101 0101 + 1 = 1101 0110
- 4Shortcut: keep the bits up to the rightmost 1 (10) and flip the rest0010 1010 → 1101 0110
- 5Check: add the weights of the 1 bits, with the leftmost one negative−128 + 64 + 16 + 4 + 2 = −42
−42 at each standard width
| Width | Hex | Range |
|---|---|---|
| 4-bit | Does not fit | |
| 8-bit | 0xD6−128 to 127 | −128 to 127 |
| 16-bit | 0xFFD6−32,768 to 32,767 | −32,768 to 32,767 |
| 32-bit | 0xFFFF FFD6−2,147,483,648 to 2,147,483,647 | −2,147,483,648 to 2,147,483,647 |
| 64-bit | 0xFFFF FFFF FFFF FFD6−2⁶³ to 2⁶³ − 1 | −2⁶³ to 2⁶³ − 1 |
A wider copy fills the new high bits with 1s (F in hex), the sign bit copied. This is sign extension.
Add a number
Convert a list
Each line
| Decimal | 8-bit pattern | Hex |
|---|---|---|
| −1 | 1111 1111Hex 0xFF | 0xFF |
| −42 | 1101 0110Hex 0xD6 | 0xD6 |
| 100 | 0110 0100Hex 0x64 | 0x64 |
| −128 | 1000 0000Hex 0x80 | 0x80 |
How it's worked out
Two’s complement is how computers store whole numbers that can be negative. A fixed number of bits, the width w, holds each number. Every bit has its usual place value, a power of 2, except the leftmost bit, which counts as negative. The value of a pattern is the sum of the weights of its 1 bits:
Here is the bit in position i, counted from 0 at the right. A leading 0 means the number is 0 or positive, and the pattern is plain binary. A leading 1 means it is negative.
To store a negative number, write its size in binary at the full width, invert every bit, and add 1. Reading a pattern back works the same way in reverse, or you can read it as an ordinary unsigned number and subtract 2 to the power w when the leading bit is 1.
The range for w bits runs from to . That is −128 to 127 at 8 bits, −32,768 to 32,767 at 16 bits and −2,147,483,648 to 2,147,483,647 at 32 bits. There is one more negative value than positive, because zero takes one of the patterns that start with 0.
The method is used everywhere because the adder needs no special case for signs. In 8 bits, −5 + 5 gives a ninth carry bit that is simply dropped, and what is left is 0. Since the C23 standard, C allows no other way to store signed integers.
Worked examples
A homework question: −42 in 8 bits
Write 42 in binary at 8 bits: 0010 1010. Invert every bit to get 1101 0101, then add 1. The answer is 1101 0110, or 0xD6 in hex.
To check, add the weights of the 1 bits, with the leftmost one negative: −128 + 64 + 16 + 4 + 2 = −42. The same bits read as an unsigned byte are 214, which is why one byte can mean two numbers.
A 32-bit register value of 0xFFFFFF85
Sam sees 0xFFFFFF85 in a 32-bit field of a debugger. F in hex is 1111, so the leftmost bit is 1 and the number is negative. Read as unsigned it is 4,294,967,173. Subtract 2 to the power 32 and it is −123.
A long run of F digits at the front is a quick sign of a small negative number. The value sits in the last digits, and the leading F digits are the sign bit copied into a wide field.
A byte counter that jumps to a negative number
A score is kept in a signed byte and stands at 100. Add 50 and the true sum is 150, but the largest 8-bit value is 127. The adder writes 1001 0110, which starts with 1, so the program reads −106.
This is signed overflow. It can only happen when both numbers have the same sign and the result has the other sign. Java wraps around like this. The C standard leaves signed overflow without a defined result, so a compiler may assume it never happens. A 16-bit type holds the sum with room to spare.
Reading your result
The big pattern is your number at the width you chose, in groups of four bits so it lines up with hex digits. Under it is the hex and the unsigned reading of the same bits. When you read bits instead, the big number is the signed value.
One’s complement and sign and magnitude are older ways to store negatives, shown for comparison. Both have two zeros and hold one value fewer than two’s complement: neither can store −128 in 8 bits, so the tool says so.
The width table shows the same value at 4, 8, 16, 32 and 64 bits. A negative value gains leading F digits as the width grows: −1 is 0xFFFF at 16 bits. That copying of the sign bit is called sign extension, and it is how a signed byte becomes a wider type without changing its value.
The smallest value at each width is special. −128 is 1000 0000, and if you invert and add 1 you get the same pattern back, because +128 does not fit in 8 bits.
Common mistakes
- Leaving out the leading bits. The width matters: 1010 typed at 8 bits is 10, because the missing bits are 0s. As a 4-bit pattern it would be negative.
- Widening with 0s instead of copying the sign. −42 at 16 bits is 1111 1111 1101 0110. Padding with 0s gives 214, a large positive number.
- Forgetting the width when reading hex. 0xFF is −1 as a signed byte but 255 as a 16-bit value.
- Adding 1 before inverting. The order is invert, then add 1. Doing it the other way gives a pattern that is off by 2.
- Thinking a carry out means overflow. For signed numbers, overflow is when two numbers of the same sign give a result of the other sign. A carry out of the top bit on its own is normal, as in −5 + 5.