# Two's Complement

> 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.

Page: https://formulabro.yunusemrevurgun.com/computer-science/twos-complement/  
Category: Computer Science  
Published: 2026-10-22  
Updated: 2026-10-22

## Formula

$$
N = -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

- **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 $2^{7}$**: A **power** of two is two multiplied by itself a set number of times, and the small raised number is the **exponent**. So $2^{7} = 128$, the place value of the leftmost bit in an 8-bit pattern, and $2^{0} = 1$, the place value of the rightmost bit. Each step down in the exponent halves the value, so $2^{6} = 64$ and $2^{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.

More notation help: https://formulabro.yunusemrevurgun.com/math-basics/

## What each symbol means

- $N$: 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 = -5$.
- $n$: The **width** of the bit pattern: how many bits it has. It is a count with no unit. An 8-bit pattern has $n = 8$, so its positions run from 0 to 7.
- $b_{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 $b_{7}$.
- $2^{n-1}$: The **place value** of the sign bit, which is $2^{n-1}$. For 8 bits it is $2^{7} = 128$. In this formula the sign bit is counted as minus this value, not plus it. It has no unit.
- $i$: The **position** of one bit, counted from 0 on the right. It has no unit. It runs from 0 up to $n - 2$ in this formula, which is every position except the leftmost.
- $b_i$: The **bit** in position $i$, 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.
- $2^{i}$: The **place value** of position $i$, which is 1, 2, 4, 8 and so on as $i$ grows. It has no unit, and each place is worth twice the place on its right.
- $\sum_{i=0}^{n-2}$: The **sum sign**, which means add up the terms for every position from 0 up to $n - 2$. It has no unit. It covers all the bits except the leftmost, sign bit.

## The formula piece by piece

- $b_{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.
- $2^{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.
- $\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 $2^{n-1} - 1$, the largest value without the sign bit.
- $-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 $2^{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 = -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 $n$ 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 $-2^{n-1}$ instead of $+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 $2^{n-1}$, and the number is negative. For 8 bits, $2^{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 $x$ plus its inverted pattern is all ones, and all ones is $-1$ in two's complement. So the inverted pattern is $-x - 1$, and adding 1 gives $-x$. For 8 bits the range runs from $-2^{7} = -128$ to $2^{7} - 1 = 127$. There is one more negative value than positive value, because $-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 $b$ from $a$ becomes adding $a$ to the two's complement of $b$. 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 $00000101$ in 8 bits, because $4 + 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 $00000101$, which gives $4 + 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 $00000101$ becomes $11111010$. Read as two's complement, $11111010$ is $-128 + 122 = -6$, which is one too small, so one more step is needed.
3. **Add 1.** Adding 1 to $11111010$ gives $11111011$. This moves the value up one step, from $-6$ to $-5$, which is the target. The pattern $11111011$ therefore stands for $-5$.
4. **Check the pattern with the formula.** The sign bit $b_{7} = 1$ is worth $-1 \cdot 2^{7} = -128$. The other seven bits, $1111011$, give $64 + 32 + 16 + 8 + 0 + 2 + 1 = 123$. Adding the two parts gives $-128 + 123 = -5$, which matches the target.
5. **Read the same bits without a sign.** Read as an ordinary binary number, $11111011$ is $128 + 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 $-2^{7} = -128$, written 10000000. The largest value is $2^{7} - 1 = 127$, written 01111111. Every whole number from $-128$ to $127$ 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**.

## Common mistakes

- Reading the leftmost bit as $+128$. In two's complement a leading 1 makes the number negative, so $11111011$ is $-5$, not $251$.
- Negating by inverting bits and forgetting the $+1$. Inverting $00000101$ gives $11111010$, which is $-6$, not $-5$.
- Assuming the range is symmetric. An 8-bit number runs from $-128$ to $127$. There is no $+128$, so negating $-128$ overflows and the result stays at $-128$.
- Mixing signed and unsigned readings. The same bits mean $251$ or $-5$. Decide which reading applies before you read the number.

## Where you will meet it

- Signed integers in most modern processors, including the x86 and ARM families.
- Programming languages such as Java, where the int and long types are two's complement integers.
- Audio samples, where 16-bit signed sound values run from $-\text{32,768}$ to $\text{32,767}$.
- Integer overflow, where $127 + 1$ in an 8-bit signed byte wraps around to $-128$.

## Questions people ask

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

That design, called sign-and-magnitude, has two zeros, $+0$ and $-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 $11111011$, inverting gives $00000100$, and adding 1 gives $00000101$, which is 5. So the number is $-5$.

### Is there a shortcut for subtraction?

Yes. To compute $a - b$, add $a$ to the two's complement of $b$. The adding circuit does all the work, so the processor needs no separate subtraction circuit.

## Related formulas

- [Binary Number Value](https://formulabro.yunusemrevurgun.com/computer-science/binary-number-value/)
- [Hexadecimal Number Value](https://formulabro.yunusemrevurgun.com/computer-science/hexadecimal-number-value/)
- [Floating-Point Value](https://formulabro.yunusemrevurgun.com/computer-science/floating-point-value/)
- [Bits Needed](https://formulabro.yunusemrevurgun.com/computer-science/bits-needed/)

## Sources

- [Two's complement, Wikipedia](https://en.wikipedia.org/wiki/Two%27s_complement)
- [Signed number representations, Wikipedia](https://en.wikipedia.org/wiki/Signed_number_representations)

From Formula Bro (https://formulabro.yunusemrevurgun.com/), a side project developed by yunusemrevurgun.com.
