The guide
A computer has no minus sign, so negative numbers are a trick played on the top bit
Two's complement gives every fixed-width integer one zero, one extra negative value and a single adder that serves signed and unsigned numbers alike.
- -128 to 127 the range of an 8-bit signed number
- 100 + 100 = -56 in 8 bits, with signed overflow
- 1111 1010 the 4-bit -6 sign-extended to 8 bits
- 1 - (-128) = 129 does not fit in 8 bits
Read the full guide Fold the guide
A processor stores bits and nothing else. It has no minus sign, no decimal point and no notion of “below zero”. Every negative number you have ever seen on a computer is a convention laid over a row of ones and zeros, and the convention that won is called two’s complement. The reason it won is practical, not elegant. It lets one adder circuit handle signed and unsigned numbers, it has a single zero, and it turns subtraction into addition. Almost every consequence worth knowing, from the lopsided range to the overflow rules, follows from one decision about the top bit.
One bit that counts backwards
Take a pattern of w bits. Read as an unsigned number, the bit at position k is worth 2 to the power k. Two’s complement changes exactly one weight: the top bit, at position w minus 1, is worth minus 2 to the power w minus 1. Everything else stays the same.
In four bits the weights are minus 8, 4, 2 and 1. The pattern 1010 is therefore minus 8 plus 2, which is minus 6. The pattern 0110 is 4 plus 2, which is 6. The top bit works as a sign flag, because a set top bit always pulls the total below zero, but it also carries a weight, and that is what keeps the arithmetic honest. Put differently, the pattern is the number modulo 2 to the power w. Minus 6 and 10 are the same four bits, 1010, because they differ by 16. Which one you meant depends only on how you choose to read the pattern, not on anything stored in it.
Why there is one more negative than positive
Four bits give 16 patterns. Half have the top bit clear and half have it set. Zero lives in the clear half, so that half holds zero and seven positive numbers, while the set half holds eight negative numbers. The range is minus 8 to 7. At 8 bits it is -128 to 127, and at 64 bits it is minus 9223372036854775808 to 9223372036854775807.
There is a cleaner argument. Negation pairs each number with its opposite, and zero is its own opposite. The nonzero patterns number 2 to the power w minus 1, which is odd, so they cannot all be paired off. One must be left over, and it is the most negative value. That value is its own negation, which is the source of one famous trap.
If you negate minus 128 in 8 bits, the answer is minus 128 again. The true answer, 128, has no 8-bit pattern. A routine that negates an arbitrary integer and assumes the result has the opposite sign has just met this case.
Flip every bit, then add one
The standard recipe for the negative of a number is to invert all its bits and add one, discarding any carry out of the top. For 6 in four bits: 0110 inverts to 1001, and adding one gives 1010, which is minus 6. The recipe works because a pattern plus its inversion is all ones, which reads as minus one, so the inversion is minus the number minus one, and adding one repairs the offset.
The recipe fails in exactly one place, and now you can see why. Invert 1000 0000 and you get 0111 1111. Add one and you are back at 1000 0000. The carry that should have signalled trouble never leaves the top, and instead it lands in the sign bit. That pattern is the signature of an overflow. Zero shows the opposite behaviour. Invert 0000 and you get 1111, add one, and the carry runs all the way out of the top and is thrown away, leaving 0000. Dropping that carry is part of the rule, not a bug. A calculator that skips the drop, or that only flips the bits, reports minus 7 where the answer should be minus 6.
Carry and overflow are not the same thing
Two flags describe an addition, and people mix them up. The carry out says that the sum of the two patterns, read as unsigned numbers, did not fit in the width. The signed overflow flag says that the sum of the two numbers, read as signed values, left the range. A hardware adder produces both from the same bits, and they answer different questions.
The rule for signed overflow is about signs. If you add two numbers that are both non-negative and the result looks negative, something went wrong. If you add two negative numbers and the result looks non-negative, the same. Mixed signs can never overflow, because the sum lies between the operands. An equivalent test compares the carry into the sign bit with the carry out of it: overflow happens when they differ.
Here is the worked example you can reproduce in the tool. Set the width to 8 and add 100 and 100. In binary that is 0110 0100 plus 0110 0100, which gives 1100 1000. Read as unsigned, that is 200, which fits, so there is no carry out. Read as signed, it is minus 56, but the true sum is 200, above the maximum of 127. The two operands were non-negative and the result is negative, so signed overflow is set. The tool prints this as 100 + 100 = -56.
Now add minus 1 and 1 in the same width. The patterns are 1111 1111 and 0000 0001, and the sum is 0000 0000 with a carry out of the top. The carry out is set, yet the signed answer, zero, is exactly right. This is the everyday case, and it is why a carry flag cannot stand in for an overflow flag.
Subtraction reuses the adder. To compute A minus B, the hardware adds A to the inverted B with a carry in of one. The overflow test changes shape, though. For a subtraction, overflow occurs when the operands have different signs and the result has the sign of B rather than of A. Check A plus the negated B with the addition rule alone, and 1 minus minus 128 in 8 bits slips through, because the negated B is minus 128 again. The true answer, 1 - (-128) = 129, does not fit.
Widening and narrowing
Moving a number into a wider register is where the top bit earns its keep. To widen a two’s complement pattern, copy the top bit into every new high position. Minus 6 in four bits is 1010, and in eight bits it is 1111 1010. The weights of the added bits sum to the weight the old sign bit used to carry, so the value is unchanged. A positive number gets zeros, which is the same copy rule.
Zero extension, by contrast, always adds zeros. That is correct for an unsigned number and wrong for a negative signed one: zero-extending 1010 to eight bits gives 0000 1010, which is 10, not minus 6. Languages and instruction sets keep the two operations apart for exactly this reason, and the tool offers them as separate actions.
Narrowing goes the other way. Truncation keeps the low bits and drops the rest, and it preserves the value only when the dropped bits were all copies of the new top bit. Otherwise the number changes, and a safe program must check first. The GCC manual says a conversion to a narrower type reduces the value modulo 2 to the power of the new width and raises no signal, so the same wrap that sums use is applied to a conversion.
Where the convention shows up
You meet two’s complement in file formats and wire protocols as well as in registers. The XDR standard describes its signed 32-bit integer as two’s complement notation with the most significant byte first. Python’s struct module packs signed integers of 1, 2, 4 and 8 bytes, and its bitwise operators behave as though a negative number had an infinite string of sign bits to the left.
Compilers have settled the question too. The GCC manual states that it supports only two’s complement integer types and notes that a recent C standard requires them. Rust documents its signed types with the range minus 2 to the power 7 up to 2 to the power 7 minus 1 for 8 bits, and offers an addition that returns both the wrapped value and a flag saying an overflow occurred. That pairing, a wrapped result plus a flag, is the honest design, and this calculator follows it.
Overflow is a security matter as well as an arithmetic one. The weakness catalogued as CWE-190 describes a calculation whose result is too large to store, so that the value may become very small or negative. The entry notes that such overflows are most often cited for memory-corruption bugs, though they can matter elsewhere.
What this method cannot do
Two’s complement is a rule for whole numbers in a fixed width. It says nothing about fractions or floating point, which use a different layout. It does not decide what a program should do after an overflow: some systems wrap and some trap, and the GCC manual has one flag that tells the compiler to assume signed overflow wraps and another that makes it trap, noting that the wrapping flag enables some optimizations and disables others.
The calculator also stops at 64 bits and at entries of 80 characters. It works with exact integers rather than the double-precision numbers that a typical scripting language uses by default, because those lose exactness above 2 to the power 53. Its self check runs in a web worker, a background thread, and compares packed layouts with the bytes a DataView writes. It never runs a program for you, and it does not guess what width your data has. You choose the width, and every result is reported against that choice.
The lasting lesson is that a pattern of bits has no meaning until you say how it is read. The same eight bits are 200, minus 56 or a letter, depending on the reader. A bug that mixes the readings is a bug about conventions, and the quickest way to find it is to look at every bit and every weight at once.
Sources
Every source consulted for this guide. Links checked November 9, 2026.
- Two's complement Wikipedia
- Sign extension Wikipedia
- Integer overflow Wikipedia
- BigInt MDN Web Docs
- BigInt.asIntN() MDN Web Docs
- DataView MDN Web Docs
- Web Workers API MDN Web Docs
- Integers implementation GNU Compiler Collection manual, Free Software Foundation
- Code Gen Options GNU Compiler Collection manual, Free Software Foundation
- Numeric types The Rust Reference, The Rust Project
- Primitive type i8 Rust standard library documentation, The Rust Project
- XDR: External Data Representation Standard RFC 4506, Internet Engineering Task Force
- Built-in Types Python Software Foundation
- The struct module Python Software Foundation
- CWE-190: Integer Overflow or Wraparound The MITRE Corporation