Two's Complement Explained: How Signed Integers Are Stored in Binary
Two's complement is the storage convention behind every signed integer on modern hardware: a negative value is the bitwise inversion of its positive magnitude plus one, and the high bit acts as the sign.
In brief
- What is it?
- Two's complement is the scheme used to store signed integers in binary: the bits of a negative number are the bitwise inversion of its positive magnitude plus one, and a set high bit means negative.
- Who is it for?
- Developers debugging hex dumps, bit fields, overflow bugs and binary protocols where the pattern 0xFF might mean −1 or 255 depending on interpretation.
- How DataFormatter's tool is different
- The Developer Calculator converts integer literals to exact two's-complement bits at 8, 16, 32 or 64 bits, interprets them back to signed and unsigned values, and flags overflow while still showing the truncated pattern.
Two's complement is a convention: a fixed number of bits interprets its high bit as a negative sign, and every negative value is produced from its positive magnitude by inverting all bits and adding one. Because the same binary pattern can be read as signed or unsigned, the same bits in memory can mean 255 or −1 — the interpretation is what makes the difference.
The rule: invert every bit and add one
To find the two's-complement bits of a negative number, write the positive magnitude in binary, flip every bit, then add 1. For −42 at 8 bits: 42 is 00101010, inverted it is 11010101, plus one gives 11010110. The all-ones pattern 11111111 is −1 (invert 00000001, add 1, and 00000000 + 1 rolls over to 11111111 by definition).
42 00101010
invert 11010101
+ 1 11010110unsigned: 214
signed: −42
hex: 0xD6Why it wins over the alternatives
Two's complement makes ordinary addition work for both signs with the same hardware. Add 1 to 11111111 (≈ −1) and you get 00000000 (0) with a discarded carry — exactly what −1 + 1 should be. There is a single representation of zero (00000000), unlike sign-magnitude where 10000000 would be “minus zero”, and subtraction becomes addition of the complemented value.
Reading a pattern in your head
| Pattern | Unsigned | Two's-complement signed |
|---|---|---|
| 0000 | 0 | 0 |
| 0111 | 7 | 7 (max positive) |
| 1000 | 8 | -8 (min negative) |
| 1001 | 9 | -7 |
| 1111 | 15 | -1 |
If the high bit of a signed value is 0, the number is positive and the remaining bits are its magnitude. If the high bit is 1, the number is negative: 11111111 at 8 bits is −1, 10000000 is −128, and the range between them covers exactly −128..127 — one more negative value than positive, because zero consumes one pattern.
The hex shortcut
At 8-bit signed widths the high hex nibble tells most of the story: 0x00–0x7F are 0..127 and 0x80–0xFF are −128..−1. So 0xFF is −1, 0xFE is −2, and 0x80 is −128. The same shortcut works at 16 bits with 0x8000 (−32768) and 0xFFFF (−1).
What happens outside the range
When a value does not fit the requested width, the calculator truncates to the low bits and reports overflow — it still shows exactly what bits a machine of that width would keep. −300 at 8 bits, for example, becomes 11010100 (−44 when read signed). This matches the chip: overflow drops the high bits and the low pattern is what lands in memory.
Try it
Open the Developer Calculator's two's-complement tool, enter negative and positive values at 8, 16, 32 or 64 bits, and read the bits, hex and signed interpretation. Overflow widths warn but still display the truncated pattern.
Frequently asked questions
How do you negate a two's-complement value?
Same rule: invert every bit and add one. Applying it twice returns the original value, so it works symmetrically for positive and negative inputs.
Why is the minimum −128 at 8 bits and not −127?
0b10000000 is the only pattern with no positive counterpart — all other negative patterns come in +/− pairs, but that one represents −2^7, leaving the space for zero.
Is 0xFF always −1?
Only when interpreted as an 8-bit signed value. As an 8-bit unsigned value the same pattern is 255, which is precisely why keeping signed and unsigned straight matters.