Two's Complement Representation

A computer stores an integer in a fixed number of bits, so it needs a rule that says which bit patterns stand for negative numbers. Almost every modern processor uses the same rule. You meet it whenever you read a memory dump, cast between integer types in C or Java, or trace a subtraction through an ALU. This lesson shows how the bits are weighted, how to negate a value, and how to tell when a sum no longer fits.

Weighting the sign bit

In two's complement, an nn-bit pattern is read like an unsigned binary number, except that the most significant bit (MSB) has a negative weight. For bits bn−1bn−2…b1b0b_{n-1} b_{n-2} \dots b_1 b_0 the value is


x=−bn−1 2n−1+∑i=0n−2bi 2ix = -b_{n-1}\,2^{n-1} + \sum_{i=0}^{n-2} b_i\,2^{i}


The MSB therefore works as a sign bit: it is 0 for zero and positive values and 1 for negative values. Zero has exactly one pattern, all zeros, so the remaining patterns split unevenly and the range runs from −2n−1-2^{n-1} to 2n−1−12^{n-1}-1.


Example: In 4 bits, 1011 is −8+0+2+1=−5-8 + 0 + 2 + 1 = -5, and 0110 is 0+4+2+0=60 + 4 + 2 + 0 = 6.


Width

Smallest value

Largest value

Patterns

4 bits

−8

7

16

8 bits

−128

127

256

16 bits

−32768

32767

65536

32 bits

−2147483648

2147483647

4294967296


To move a value into a wider register, copy the sign bit into every new position. This sign extension keeps the value unchanged: 1011 (−5) becomes 1111 1011 in 8 bits.

Negating a value

Write xˉ\bar{x} for the pattern xx with every bit inverted. A pattern plus its inverse is all ones, and all ones is −1 in two's complement. Therefore


xˉ+1=(−1−x)+1=−x\bar{x} + 1 = (-1 - x) + 1 = -x


To negate, invert every bit and add 1.


Example: 5 is 0101 in 4 bits. Inverting gives 1010, and adding 1 gives 1011. The weighting above confirms that 1011 is −5. Applying the same two steps to 1011 returns 0101.


A quicker hand method gives the same result. Copy the bits from the right up to and including the first 1, then invert every bit to its left. For 0110 (6), keep 10, invert 01 to 10, and read 1010, which is −6.



Adding and detecting overflow

Two's complement addition uses the same adder circuit as unsigned addition. Add the patterns column by column and discard any carry out of the MSB. Subtraction adds the negated operand, a−b=a+(bˉ+1)a - b = a + (\bar{b} + 1), so one circuit handles both operations.


Example: For −2 + 3 in 4 bits, 1110 + 0011 = 1 0001. Dropping the fifth bit leaves 0001, which is 1, the correct sum.


Overflow occurs when the true result lies outside the representable range, so the stored pattern means a different number. Overflow is possible only when both operands have the same sign. Either of two equivalent tests detects it:

  • the sign bit of the result differs from the shared sign of the operands, or
  • the carry into the MSB differs from the carry out of the MSB.

Example: 6 + 3 in 4 bits gives 0110 + 0011 = 1001, which reads as −7. Two positive operands produced a negative result, so the sum overflowed: the true value 9 exceeds the largest 4-bit value, 7. The carry into the MSB is 1 and the carry out is 0, so the carry test agrees.


Operand signs

Can the sum overflow?

Sign of an overflowed result

Both non-negative

Yes

Negative

Both negative

Yes

Non-negative

One of each

No

Not applicable


A circle of the sixteen 4-bit patterns with each signed value written just inside the circle and the boundary between 0111 and 1000 marked in orange.


Adding 1 moves one step clockwise around the wheel. Stepping from 0111 to 1000 jumps from the largest value to the smallest one. This is why an overflowed result differs from the true sum by exactly 2n2^n.



What to remember

  • An nn-bit two's complement pattern gives the MSB a weight of −2n−1-2^{n-1}, so the range is −2n−1-2^{n-1} to 2n−1−12^{n-1}-1.
  • Negate by inverting every bit and adding 1. The most negative value is its own negation.
  • Add the patterns as if they were unsigned and discard the carry out of the MSB.
  • Overflow needs two operands of the same sign. It shows up as a result with the opposite sign, or as a carry into the MSB that differs from the carry out.

Example: Signed corrections in an 8-bit register

A microcontroller stores temperature corrections as 8-bit two's complement integers, and its firmware adds corrections together in an 8-bit register.


a) The register holds 1110 0110. What correction does it store?


The MSB is 1, so it contributes −27=−128-2^7 = -128. The remaining 1 bits have weights 64, 32, 4 and 2, which sum to ________.


x=−128+64+32+4+2=−26x = -128 + 64 + 32 + 4 + 2 = -26


The register stores a correction of −26.


b) Write the correction −45 as an 8-bit two's complement pattern.


Start from +45 = 32 + 8 + 4 + 1, which is ________ in 8 bits. Inverting every bit gives ________, and adding 1 gives 1101 0011.


Check by weighting the bits:


−128+64+16+2+1=−45-128 + 64 + 16 + 2 + 1 = -45


The pattern for −45 is 1101 0011.


c) The firmware adds the corrections from parts a) and b). Find the 8-bit result and decide whether the addition overflowed.


Add the two patterns exactly as unsigned binary numbers:


1110 0110+  1101 00111  1011 1001\begin{array}{r} 1110\,0110 \\ +\;1101\,0011 \\ \hline 1\;1011\,1001 \end{array}


The ninth bit is a carry out of the MSB, and it is discarded. That leaves 1011 1001, whose value is −128+32+16+8+1=−71-128 + 32 + 16 + 8 + 1 = -71. This matches −26+(−45)=−71-26 + (-45) = -71.


Both operands are negative and the result is negative, so there is no overflow. The carry test agrees. The low seven bits of the operands are worth 102 and 83, and their sum of 185 is at least 128, so the carry into the MSB is ________. That matches the carry out of 1.


The register holds 1011 1001, which is −71, with no overflow.


d) A further correction of −90 is added to the result of part c). Find the stored pattern and its value, and explain what went wrong.


Encode −90 first. +90 = 64 + 16 + 8 + 2 is 0101 1010; inverting gives 1010 0101, and adding 1 gives ________.


Now add the patterns:


1011 1001+  1010 01101  0101 1111\begin{array}{r} 1011\,1001 \\ +\;1010\,0110 \\ \hline 1\;0101\,1111 \end{array}


Discarding the carry out leaves 0101 1111, which reads as 64+16+8+4+2+1=9564 + 16 + 8 + 4 + 2 + 1 = 95.


Two negative operands produced a positive result, so the addition overflowed. The true sum is −71+(−90)=−161-71 + (-90) = -161, which is below −128, the smallest 8-bit value. The stored value is the true sum shifted by 282^8: −161+256=95-161 + 256 = 95.


The carry test gives the same verdict. The low seven bits are worth 57 and 38, which sum to less than 128, so the carry into the MSB is ________ while the carry out is 1.


The register holds 0101 1111, which reads as +95, and the result is wrong because the signed addition overflowed.

A sensor interface stores each sample as a 12-bit two's complement integer. Which range of whole-number values can a single sample represent?

Choose an answer

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

An 8-bit processor has an adder but no subtractor, so it computes A−BA - B by adding the two's complement of BB to AA. Put the steps of this procedure in the order the processor carries them out, starting from the original operand BB.

  1. 1.

    Add the pattern for −B-B to AA and keep only the low 8 bits.

  2. 2.

    Compare the sign bits of the two adder inputs with the sign bit of the result to detect overflow.

  3. 3.

    Write BB as an 8-bit binary pattern.

  4. 4.

    Add 1 to the inverted pattern to form −B-B.

  5. 5.

    Invert every bit of the pattern for BB.

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

A program must store the temperature offset −76 in an 8-bit two's complement register. Write the 8-bit pattern that the register holds, as eight binary digits.

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

Extra Practice