Two’s complement calculator
Turn a signed number into its two’s complement bits, or read a bit pattern back as a signed number.
How two’s complement works
Computers store negative whole numbers by letting the top bit count as a negative place value. In 8 bits the places are −128, 64, 32, 16, 8, 4, 2 and 1, so 11101110 is −128 + 64 + 32 + 8 + 4 + 2 = −18.
To write a negative number, write its size in binary, flip every bit and add 1. Reading one back works the same way: flip every bit, add 1, and put a minus sign in front.
8-bit examples
| Decimal | 8-bit pattern | Hex |
|---|---|---|
| 127 | 0111 1111 | 7F |
| 1 | 0000 0001 | 01 |
| 0 | 0000 0000 | 00 |
| -1 | 1111 1111 | FF |
| -2 | 1111 1110 | FE |
| -18 | 1110 1110 | EE |
| -127 | 1000 0001 | 81 |
| -128 | 1000 0000 | 80 |
One’s complement vs two’s complement
The one’s complement of a binary number is every bit flipped. Add 1 to that and you have the two’s complement, which is how computers store negative whole numbers.
| Number | 8-bit binary | One’s complement (flip) | Two’s complement (flip, add 1) |
|---|---|---|---|
| 5 | 00000101 | 11111010 | 11111011 |
| 18 | 00010010 | 11101101 | 11101110 |
| 0 | 00000000 | 11111111 | 00000000 (the carry is dropped) |
Using one’s complement on its own to mean “negative” has a problem: zero has two forms, 00000000 (+0) and 11111111 (−0). Two’s complement has a single zero, which is one reason it won. One’s complement arithmetic is still used in one well-known place: the Internet Protocol header checksum is defined as the 16-bit one’s complement of the one’s complement sum of the header’s 16-bit words (RFC 791, section 3.1).
Sources and method
- W3C WebAssembly Core Specification, overview: integers are read as unsigned or as signed in two’s complement representation. Checked 2026-11-25.
- RFC 791 (STD 5), Internet Protocol, section 3.1, Header Checksum: the 16-bit one’s complement of the one’s complement sum of the header’s 16-bit words. Checked 2026-11-25.
Conversions use exact whole-number arithmetic (JavaScript BigInt), so long numbers keep every digit. Fractions are kept as exact ratios; when the digits repeat, the repeating block is found and marked instead of rounded.
Questions
Why does −128 fit in 8 bits but +128 doesn’t?
Zero takes one of the 128 patterns that start with 0, leaving 0 to 127 for the positives. All 128 patterns that start with 1 are negative, from −128 to −1.
How do I extend a two’s complement number to more bits?
Copy the top bit into the new places (sign extension). −18 is 11101110 in 8 bits and 1111111111101110 in 16 bits.
Is this the same as one’s complement?
No. One’s complement only flips the bits and has two zeros (00000000 and 11111111). Two’s complement adds 1 after flipping, which leaves a single zero and lets the same adder handle signed and unsigned numbers.
What is one’s complement?
Every bit of the number flipped: 0 becomes 1 and 1 becomes 0. Adding 1 to the one’s complement gives the two’s complement.