Skip to the tool
Binary Translator Free tools

Two’s complement calculator

Turn a signed number into its two’s complement bits, or read a bit pattern back as a signed number.

Updated · Written and checked by Murugan Vellaichamy

Waiting for input

Result
Try:

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

Decimal8-bit patternHex
1270111 11117F
10000 000101
00000 000000
-11111 1111FF
-21111 1110FE
-181110 1110EE
-1271000 000181
-1281000 000080

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.

Number8-bit binaryOne’s complement (flip)Two’s complement (flip, add 1)
5000001011111101011111011
18000100101110110111101110
0000000001111111100000000 (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

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.

More converters

Copied