Developer

What Is Two's Complement? Representing Negatives, Invert-and-Add-One, and Ranges

Computers have no minus sign and represent negative numbers using only the bits 0 and 1. The key to this is two's complement. This article explains what two's complement is, why you invert the bits and add one, the role of the sign bit, overflow, and the ranges representable in 8, 16, and 32 bits.

Published: 2026-10-16

When working with integers in code, you may run into situations where a negative value turns into an unexpectedly huge number, or a calculation wraps around so the sign flips. Behind this is two's complement, the mechanism computers use to represent negative numbers. A computer has no minus sign "-" and must express negative numbers using only 0s and 1s. This article explains, step by step, what two's complement is, why you invert the bits and add one, and how the bit width determines the range of numbers you can represent.

What Is Two's Complement

Two's complement is the scheme computers use internally to represent negative integers. In the numbers we use every day, we put a "-" in front to show a negative value, but a computer can only handle sequences of the bits 0 and 1. So the idea is that, within a fixed bit width, the value that "overflows back to 0 when added" to a number is treated as that number's negative.

For example, in an 8-bit world the representable values are the 256 patterns from 0 to 255. Two's complement treats these 256 patterns as a single ring and assigns the latter half of the ring (128 and above) to the negative region. Concretely, the rule is: if the leading bit (the most significant bit) is 1 the value is negative, and if it is 0 the value is zero or positive.

The biggest advantage of this scheme is that the same addition circuit can be reused for subtraction. Negative numbers need no special handling and can be computed with the same hardware as addition, which is why nearly every modern computer uses two's complement for integers. To get a feel for what number a sequence of bits reads as, it helps to move back and forth between binary and decimal in the Number Base Converter.

Why Represent Negatives with a Complement

There are a few ways to represent negative numbers besides two's complement. The well-known ones are sign-and-magnitude, which uses the leading bit as a plain sign, and one's complement, which inverts all the bits. But these have awkward weaknesses.

  • Sign-and-magnitude: it produces two kinds of zero, "+0" and "-0," and addition has to branch based on the sign.
  • One's complement: it also has two zeros, +0 and -0, and requires special handling to add the overflow (carry) back to the lowest bit.

By contrast, with two's complement the representation of zero is unique, so there is no +0 versus -0 distinction. Even more importantly, positives and negatives can be computed with exactly the same addition procedure, without distinguishing them. For instance, to compute "5 + (-3)," you simply add the two's complement of -3 to 5 and naturally get the correct answer of 2. Because subtraction can be replaced by "adding a negative," the hardware can be kept simple.

In short, two's complement is chosen for its design rationality: zero is unique, and the addition circuit can be shared.

Making a Two's Complement: Invert and Add One

The procedure to find the two's complement of a number (that is, the value with its sign flipped) is very simple: invert every bit (swap 0 and 1), then add 1 to the result. That is all. Let us flip the sign of "3" in 8 bits to make "-3."

3 = 0000 0011
invert the bits → 1111 1100
add 1 → 1111 1101 = -3

Conversely, if you invert this "1111 1101" again and add 1, you return to the original "0000 0011 = 3." Doing the same operation a second time brings you back, which matches the mathematical relationship "-(-3) = 3" exactly.

Why does "invert and add one" work? The inverted value is called the "one's complement," and adding it to the original number makes every bit 1. In 8 bits that is "1111 1111," which is 255. Adding one more gives 256, which exceeds the 8-bit frame and overflows, so the result returns to 0. The value that becomes 0 when added is exactly the negative, so inverting and adding one yields precisely the sign-flipped number.

This whole flow is easier to grasp by manipulating bits than by following it in your head. Run NOT (invert) in the Bitwise Calculator, add 1 to the result, and confirm that the sign indeed flips.

The Sign Bit, Bit Width, and Representable Range

In two's complement, the most significant bit (the leftmost bit) acts as the sign bit. If this sign bit is 0 the value is zero or positive; if it is 1 the value is negative. Unlike sign-and-magnitude, however, the sign bit is not just a marker; it is built directly into the calculation that determines the number's magnitude.

The representable range is determined by the bit width. An n-bit two's complement can represent from -2^(n-1) to +2^(n-1) - 1. The negative side is wider by one because zero is counted on the positive side. The ranges for common bit widths are as follows.

Bit widthRepresentable range (signed)
8-bit-128 to 127
16-bit-32,768 to 32,767
32-bit-2,147,483,648 to 2,147,483,647
64-bitabout -9.2×10^18 to 9.2×10^18

You will notice that the maximum and minimum of programming-language types like "int" and "long" line up exactly with these ranges. The upper limit of a 32-bit integer, "2,147,483,647," is a familiar number seen in many systems. Note that the same bit sequence can mean different things depending on whether you read it as unsigned (zero and up only) or signed (two's complement). Viewing the same binary as both signed and unsigned in the Number Base Converter makes this difference vivid.

Overflow and Cautions

Because the range a two's complement can represent has an upper and lower bound, an overflow occurs when a result exceeds that frame. Adding 1 to the maximum 127 of an 8-bit signed integer changes the bits from "0111 1111" to "1000 0000," which two's complement reads as -128. In other words, the instant you exceed the maximum, you jump all the way around to the minimum.

This phenomenon of a number warping to the opposite side at the edge of the ring is called wrap-around. Unintended overflow causes real bugs, such as a game score going negative or a time calculation breaking. Many historically famous bugs also stem from this integer range overflow.

  • Exceeding the range in addition or multiplication: adding two large positive numbers can turn the result negative.
  • Negating the minimum value: there is a special case where inverting -128 in 8 bits and adding 1 stays -128, a consequence of the negative side being wider by one.

Countermeasures include choosing a type with a sufficiently large bit width, checking the range before calculating, and using language features that detect overflow. Calculating values near the boundary while switching bit widths in the Bitwise Calculator lets you see exactly when a value wraps around.

Understanding by Doing

Two's complement is a topic that rarely clicks from reading alone. Actually inverting bits, adding one, and watching the sign change with your own eyes is the fastest route to understanding. Try the following steps.

  • In the Number Base Converter, convert any positive integer you like to binary.
  • In the Bitwise Calculator, apply NOT (invert) to that binary and add 1 to the result.
  • Reread the resulting bits as a signed decimal and confirm that the sign has flipped.

Repeating this round trip a few times gives you an intuitive grasp of why "invert and add one" means exactly "make it negative." Two's complement is a well-crafted device for representing negatives and zero exactly with limited bits, and for handling addition and subtraction with the same mechanism.

Please note that this article is intended to provide general information. The exact bit width of integer types and the behavior on overflow can differ by programming language and implementation. In real development, always check the latest official specifications and documentation for the language and platform you use.

FAQ

What is two's complement?

Two's complement is the scheme computers use to represent negative integers with only the bits 0 and 1. Within a fixed bit width, the value that overflows back to 0 when added is treated as a number's negative. A leading bit of 1 means negative and 0 means zero or positive, and nearly every modern computer uses it for integers.

How do you make a two's complement?

You simply invert every bit (swap 0 and 1) and add 1 to the result. For example, inverting 8-bit 3 (0000 0011) gives 1111 1100, and adding 1 gives 1111 1101, which represents -3. Doing the same operation again returns you to the original value.

Why does inverting the bits and adding one give a negative number?

Adding the original number to its inverted value (the one's complement) makes every bit 1, which is 255 in 8 bits. Adding one more gives 256, which exceeds the bit width and overflows back to 0. The value that becomes 0 when added is exactly the negative, so inverting and adding one yields the sign-flipped number.

What ranges can 8-bit or 32-bit represent?

An n-bit two's complement represents from -2^(n-1) to +2^(n-1)-1. So 8-bit is -128 to 127, 16-bit is -32,768 to 32,767, and 32-bit is -2,147,483,648 to 2,147,483,647. The negative side is wider by one because zero is counted on the positive side.

Do the WAZA TOOLS bit tools send my data anywhere?

WAZA TOOLS' Bitwise Calculator and Number Base Converter run entirely in your browser, so the values you enter are never sent to or stored on a server. They work on smartphones too, and you can try bit inversion and base conversion as many times as you like, for free.