Formula Bro

Math and physics formulas, explained like you are new.


Two's Complement

In one sentence: Two’s complement is the usual way computers write negative whole numbers in binary, and this formula reads a bit pattern as a positive or negative number.

N=−bn−1⋅2n−1+∑i=0n−2bi⋅2iN = -b_{n-1} \cdot 2^{n-1} + \sum_{i=0}^{n-2} b_i \cdot 2^{i}

Read it out loud: “N equals minus b sub n minus 1, times 2 to the power of n minus 1, plus the sum, for every position i from 0 up to n minus 2, of b sub i times 2 to the power of i. N is the signed whole number, which can be negative. n is how many bits the pattern has. b sub n minus 1 is the leftmost bit, the sign bit. The other bits b sub i are read in the ordinary binary way, and the sigma sign means add up all those terms.”

Before you start

The ideas and signs this page uses, explained from zero. For powers of ten, Greek letters and other notation in general, see Math basics.

Bit patterns with a fixed width

A computer stores a number as a bit pattern with a fixed number of bits, called its width. An 8-bit pattern always has exactly 8 positions, each holding a 0 or a 1, and the leading zeros are written out. The same pattern can be read in different ways, so the reader must know the rule that applies, as the explanation below shows.

Negative numbers

A negative number is less than zero. Everyday examples are a bank account that is overdrawn, or a temperature below freezing. A plain row of bits has no minus sign to write, so the computer needs a rule that uses the bits themselves to say the number is negative. Two’s complement is one such rule.

The sign bit

The sign bit is the leftmost bit of a signed pattern. In two’s complement, a sign bit of 0 means the number is zero or positive, and a sign bit of 1 means it is negative. The sign bit does not work like an ordinary bit. It is worth a negative amount, as the formula shows.

Powers of two, like 272^{7}

A power of two is two multiplied by itself a set number of times, and the small raised number is the exponent. So 27=1282^{7} = 128, the place value of the leftmost bit in an 8-bit pattern, and 20=12^{0} = 1, the place value of the rightmost bit. Each step down in the exponent halves the value, so 26=642^{6} = 64 and 25=322^{5} = 32.

Inverting bits

Inverting a bit means flipping it: a 0 becomes a 1, and a 1 becomes a 0. To invert a whole pattern, invert every bit at once. The pattern 00000101 becomes 11111010 when inverted. Inverting is the first step of the negation method, and it is not the same as subtracting from zero.

Wrap-around and overflow

A counter with a fixed width wraps around: when it goes past its largest value, it returns to its smallest value and keeps counting. This is called overflow when a result is too large for its width. In an 8-bit signed pattern, adding 1 to 127 wraps around to −128. The wrap-around is what makes the negative numbers line up with the positive ones.

What each symbol means

SymbolMeaning

NN

The signed whole number that the bit pattern stands for. It has no unit, because it is a plain count, and it can be negative. The pattern 11111011 stands for N=−5N = -5.

nn

The width of the bit pattern: how many bits it has. It is a count with no unit. An 8-bit pattern has n=8n = 8, so its positions run from 0 to 7.

bn−1b_{n-1}

The sign bit, the leftmost bit of the pattern. It is either 0 or 1 and has no unit. If it is 1, the number is negative. For an 8-bit pattern it is b7b_{7}.

2n−12^{n-1}

The place value of the sign bit, which is 2n−12^{n-1}. For 8 bits it is 27=1282^{7} = 128. In this formula the sign bit is counted as minus this value, not plus it. It has no unit.

ii

The position of one bit, counted from 0 on the right. It has no unit. It runs from 0 up to n−2n - 2 in this formula, which is every position except the leftmost.

bib_i

The bit in position ii, which is either 0 or 1 and has no unit. Each of these bits is read in the ordinary way, with a positive place value.

2i2^{i}

The place value of position ii, which is 1, 2, 4, 8 and so on as ii grows. It has no unit, and each place is worth twice the place on its right.

∑i=0n−2\sum_{i=0}^{n-2}

The sum sign, which means add up the terms for every position from 0 up to n−2n - 2. It has no unit. It covers all the bits except the leftmost, sign bit.

The formula piece by piece

bib_{i}

One ordinary bit, in a position other than the leftmost. It is 0 or 1 and acts like an on-off switch, so it adds its place value only when it is 1.

2i2^{i}

The place value of that position: 1, 2, 4, 8 and so on. These are the same place values as in plain binary, so the ordinary bits are read exactly as they would be without a sign.

∑i=0n−2bi⋅2i\sum_{i=0}^{n-2} b_{i} \cdot 2^{i}

Add the ordinary bits, each multiplied by its place value, for every position except the leftmost. This sum is always zero or positive, and it can reach 2n−1−12^{n-1} - 1, the largest value without the sign bit.

−bn−1⋅2n−1-b_{n-1} \cdot 2^{n-1}

The sign bit term. If the leftmost bit is 0 the term is zero. If it is 1 the term is minus 2n−12^{n-1}, which is minus 128 for 8 bits. This one term is what makes a pattern with a leading 1 count as negative.

N=−bn−1⋅2n−1+∑i=0n−2bi⋅2iN = -b_{n-1} \cdot 2^{n-1} + \sum_{i=0}^{n-2} b_i \cdot 2^{i}

The whole formula in one sentence: the signed number N equals minus the sign bit times 2 to the power of n minus 1, plus the value of all the other bits read as ordinary binary.

What the formula is doing, and why

What goes in and what comes out. You give the formula a bit pattern of nn bits, such as 11111011. It returns one signed whole number, which can be negative. The formula reads the leftmost bit in a special way and reads all the other bits as ordinary binary.

Why the leftmost bit is negative. The sign bit is worth −2n−1-2^{n-1} instead of +2n−1+2^{n-1}. If the sign bit is 0, it adds nothing, and the number is zero or positive. If it is 1, it subtracts 2n−12^{n-1}, and the number is negative. For 8 bits, 27=1282^{7} = 128, so a leading 1 subtracts 128 from the rest of the pattern. A pattern that starts with 1 is therefore always negative.

How negation works. To negate a number, invert every bit and add 1. The number xx plus its inverted pattern is all ones, and all ones is −1-1 in two’s complement. So the inverted pattern is −x−1-x - 1, and adding 1 gives −x-x. For 8 bits the range runs from −27=−128-2^{7} = -128 to 27−1=1272^{7} - 1 = 127. There is one more negative value than positive value, because −128-128 has no positive partner that fits in 8 bits.

Why this design is used. Addition works the same way for positive and negative numbers. So one adding circuit handles both, and subtracting bb from aa becomes adding aa to the two’s complement of bb. The design also has a single zero, which a plain minus-sign design does not have.

When it applies. Use this reading when the bit pattern is meant as a signed number. The same bits read without a sign give a different number, so you must know which reading applies before you read the pattern.

An everyday picture

Think of a five-digit counter that wraps around. Going one step back from 00000 lands on 99999, so 99999 can stand for the number −1. Going two steps back lands on 99998, which stands for −2, and so on. Two’s complement uses the same wrap-around on bit patterns. In 8 bits, 11111111 stands for −1 and 11111110 stands for −2.

Worked example: Storing −5 in 8 bits

Imagine a weather station that stores each temperature in one byte, 8 bits, using two’s complement. It needs to store the temperature −5 degrees. We write −5 as an 8-bit pattern, starting from +5, which is 0000010100000101 in 8 bits, because 4+1=54 + 1 = 5.

  1. Write +5 in 8 bits. Start with the ordinary binary pattern for 5. The bit in the 4 place and the bit in the 1 place are 1, and the rest are 0. So 5 is 0000010100000101, which gives 4+1=54 + 1 = 5. The leading zeros fill the eight positions and add nothing.
  2. Invert every bit. To negate a number, flip every bit, so each 0 becomes 1 and each 1 becomes 0. The pattern 0000010100000101 becomes 1111101011111010. Read as two’s complement, 1111101011111010 is −128+122=−6-128 + 122 = -6, which is one too small, so one more step is needed.
  3. Add 1. Adding 1 to 1111101011111010 gives 1111101111111011. This moves the value up one step, from −6-6 to −5-5, which is the target. The pattern 1111101111111011 therefore stands for −5-5.
  4. Check the pattern with the formula. The sign bit b7=1b_{7} = 1 is worth −1⋅27=−128-1 \cdot 2^{7} = -128. The other seven bits, 11110111111011, give 64+32+16+8+0+2+1=12364 + 32 + 16 + 8 + 0 + 2 + 1 = 123. Adding the two parts gives −128+123=−5-128 + 123 = -5, which matches the target.
  5. Read the same bits without a sign. Read as an ordinary binary number, 1111101111111011 is 128+64+32+16+8+0+2+1=251128 + 64 + 32 + 16 + 8 + 0 + 2 + 1 = 251. The two readings differ by 256, the size of the full 8-bit range. Only the rule you are using tells you which reading is correct.
  6. Find the range of 8-bit values. The smallest value is −27=−128-2^{7} = -128, written 10000000. The largest value is 27−1=1272^{7} - 1 = 127, written 01111111. Every whole number from −128-128 to 127127 has exactly one 8-bit pattern.

Answer: The 8-bit two’s complement pattern for −5 is 11111011. Read without a sign, the same bits mean 251. The range of 8-bit two’s complement numbers is −128 to 127.

The numbers in this example are recomputed by script every time the site is built.

Common mistakes

Where you will meet it

Questions people ask

Why not use the leftmost bit as a plain minus sign?

That design, called sign-and-magnitude, has two zeros, +0+0 and −0-0, and needs extra circuits to add positive and negative numbers. Two’s complement has one zero and lets the same adding circuit handle both signs.

How do I tell whether a two's complement number is negative, and how big is it?

Look at the leftmost bit. If it is 1, the number is negative. To find its size, invert the bits, then add 1 and read the result as an ordinary positive number. For 1111101111111011, inverting gives 0000010000000100, and adding 1 gives 0000010100000101, which is 5. So the number is −5-5.

Is there a shortcut for subtraction?

Yes. To compute a−ba - b, add aa to the two’s complement of bb. The adding circuit does all the work, so the processor needs no separate subtraction circuit.

Sources

  1. Two's complement, Wikipedia
  2. Signed number representations, Wikipedia

| All Computer Science formulas |

Published 2026-10-22. Plain-text copy for AI agents and readers: computer-science/twos-complement.md.