Free degree-level computing lessons for careful independent study.

Degree Level Programmes · Formal Computing Foundations · Lesson 6

Integer Representation, Overflow and Two's Complement

Reason about fixed-width integer storage and overflow.

Lesson overview

Reason about fixed-width integer storage and overflow.

CourseFundamentals of Computing
Topic strandNumber representation
Assessment styleDigital exam practice
EvidenceWorked answer plus justification

Starter: make the model explicit

Before reading the worked example, write down the objects involved, the claim being made and the notation you expect to use. This prevents the common error of calculating before modelling.

Learning objectives

  • Explain why fixed-width storage has a limited range.
  • Interpret simple unsigned and two's complement bit patterns.
  • Predict overflow in a small integer example.

Learning outcomes

  • By the end of this lesson, you can explain why fixed-width storage has a limited range.
  • By the end of this lesson, you can interpret simple unsigned and two's complement bit patterns.
  • By the end of this lesson, you can predict overflow in a small integer example.

Key vocabulary

bit widthunsigned integertwo's complementoverflow

What this lesson is about

Integer representation is about how whole numbers are stored using a fixed number of bits. Unsigned representation uses every bit pattern for non-negative values, so 8 bits can represent 0 to 255.

Two's complement is the standard signed-integer representation used by most modern processors. In an n-bit two's complement value, the leftmost bit has negative weight −2ⁿ⁻¹; the other bits keep their usual positive powers of two. That is why 11111111 is −1 in 8-bit two's complement, not 255.

Overflow occurs because fixed-width storage has a finite range. The mathematical answer may exist, but the machine representation may not have enough bits to store it.

Terms, acronyms and named methods

These are the phrases and named techniques used in this lesson. Read this section before the worked example so the notation and examples have a clear meaning.

Unsigned integerPhrase. A fixed-width integer interpretation where every bit pattern represents a non-negative value.
Two's complementNamed representation. The standard signed binary integer representation in which the leftmost bit has negative weight and the remaining bits have positive powers-of-two weights.
OverflowPhrase. A representational failure where the mathematical result is outside the range that the chosen number of bits can store.
Most significant bitPhrase. The leftmost bit in a fixed-width binary representation. In two's complement it determines the negative weight.

Formal notation and definitions

For n unsigned bits, values range from 0 to 2ⁿ − 1.For n-bit two's complement, values range from −2ⁿ⁻¹ to 2ⁿ⁻¹ − 1.In n-bit two's complement, the leftmost bit has weight −2ⁿ⁻¹ and the remaining bits have positive weights.Overflow occurs when the mathematical result is outside the representable range.

How to read the symbols

nnThe fixed number of bits available for the representation.
2n12^n-1The largest value representable by n unsigned bits.
2n1-2^{n-1}The negative weight of the leftmost bit in n-bit two's complement.
overflowA result outside the representable fixed-width range.

Use the range formula and bit weights to decode the pattern before doing arithmetic. In two's complement, the leftmost bit contributes a negative value, so 1000 in 4 bits is −8 and 1111 is −1.

Degree-level reasoning

Representation topics ask you to distinguish mathematical value from machine encoding. The same bit pattern can mean different things under different interpretation rules.

Degree-level answers should mention range, precision, rounding or overflow where those limits affect correctness.

Do not stop at a correct-looking answer. State why the method is valid, whether the result depends on a hidden assumption, and what would count as a counterexample.

Worked formal model

This section shows the model, notation, calculation and interpretation as one worked answer. The notation is part of the reasoning, not decoration.

1. Context and objects

In 4-bit two's complement, 1111 represents -1 because the leftmost bit has weight -8 while the remaining bits have weights 4, 2 and 1.

Objects: the bit width n, the bit pattern, the interpretation rule, and the representable range.

Model cue: Use two 4-bit number wheels: unsigned runs from 0 to 15, while two's complement runs from -8 to 7. The same bit pattern can therefore mean different values depending on interpretation.

2. Mathematical working

11112=8+4+2+1=11111_2 = -8 + 4 + 2 + 1 = -101112+00012=100020111_2 + 0001_2 = 1000_210002=8in 4-bit two’s complement1000_2 = -8\quad\text{in 4-bit two's complement}
Plain text version
4-bit two's complement weights: -8, 4, 2, 1
1111 = -8 + 4 + 2 + 1 = -1
0111 + 0001 = 1000, but 1000 = -8, so the intended result 8 overflowed

3. How to read the working

  1. Fix the bit width before interpreting the pattern.
  2. For unsigned integers, all bit weights are positive.
  3. For two's complement, the leftmost bit has negative weight.
  4. Overflow occurs when the intended mathematical result is outside the representable range.

4. Computing meaning and check

The bit pattern has meaning only after an interpretation rule is chosen. Overflow is a representational failure: the mathematical value exists, but the fixed-width encoding cannot hold it.

Now check: For 5-bit two's complement, state the range, decode 11111, and explain whether 01111 + 00001 overflows.

Worked example

From scenario to formal reasoning

Scenario: In 4-bit two's complement, 1111 represents -1 because the leftmost bit has weight -8 while the remaining bits have weights 4, 2 and 1.

Method: Use the definitions and notation introduced above, then state what the result means in this computing scenario.

Reveal model answer

Decode 4-bit two's complement 1111 by using weights -8, 4, 2 and 1. The value is -8 + 4 + 2 + 1 = -1. Decode 1000 as -8 + 0 + 0 + 0 = -8. Adding 0111 and 0001 gives 1000; the bit pattern is valid, but as a signed 4-bit result it means -8, so the intended mathematical sum 8 has overflowed.

Worked solution structure

How a strong answer should be written

  1. Define: State the domain and the objects under discussion. For this lesson, begin from For n unsigned bits, values range from 0 to 2ⁿ − 1.
  2. Apply: Use the relevant definition from number representation; do not rely on the diagram, wording or intuition alone.
  3. Check: Test a boundary case, counterexample candidate or representation limit.
  4. Conclude: Write one sentence that connects the formal result back to the computing scenario.

Common misconception

A common mistake is assuming mathematical integers and machine integers behave identically. Machine integers are bounded representations, and the same bit pattern can mean different values under unsigned and two's complement interpretation.

Guided practice

  1. State the range of 5-bit two's complement and decode 11111 using bit weights.
  2. Add 01111 and 00001 in 5-bit two's complement and explain the overflow.
  3. Decode the same 4-bit pattern 1111 as unsigned and as two's complement.
  4. Write one sentence explaining why fixed-width machine integers are not the same model as mathematical integers.

Quick checks

1. What is the 5-bit two's complement range?

2. In 5-bit two's complement, 01111 + 00001 overflows because:

Digital exam practice

Example exam task

For 5-bit two's complement, state the range, decode 11111, and decide whether 01111 + 00001 overflows. Explain the fixed-width representation issue.

Notation toolkit

n-bitn\text{-bit}

Means: a fixed number of bits is available

How to use: Use it before deciding the representable range.

2n1x2n11-2^{n-1}\le x\le2^{n-1}-1

Means: the n-bit two's complement range

How to use: Use it to test whether a signed result can be stored.

11\ldots

Means: a leading 1 carries negative weight in two's complement

How to use: Use it when decoding signed bit patterns.

What a good answer is expected to show

A strong answer for this lesson defines the bit width n, the bit pattern, the interpretation rule, and the representable range, applies the number representation method with visible working, and finishes by interpreting the result in the computing scenario.

How to solve it

  1. Fix the bit width before interpreting the pattern.
  2. For unsigned integers, all bit weights are positive.
  3. For two's complement, the leftmost bit has negative weight.
  4. Overflow occurs when the intended mathematical result is outside the representable range.

Model answer

Reveal model answer
5-bit two’s complement range: 24x241\text{5-bit two's complement range: }-2^4\le x\le 2^4-1111112=16+8+4+2+1=111111_2=-16+8+4+2+1=-1011112+000012=100002so 15+1 overflows01111_2+00001_2=10000_2\quad\text{so }15+1\text{ overflows}

With 5-bit two's complement, the range is -16 to 15. The pattern 11111 has value -16 + 8 + 4 + 2 + 1 = -1. The sum 01111 + 00001 represents 15 + 1 = 16, which is outside the range, so the stored 5-bit result overflows to 10000, interpreted as -16.

Practise next

  1. State the range of 5-bit two's complement and decode 11111 using bit weights.
  2. Add 01111 and 00001 in 5-bit two's complement and explain the overflow.

Self-marking criteria

  • Gives the range -16 to 15.
  • Decodes 11111 as -1 using weights.
  • Identifies 15 + 1 as outside range.
  • Explains why the stored bit pattern wraps to a negative value.
  • Distinguishes mathematical integers from machine integers.

Extension

Change one assumption in the worked scenario and decide whether the same method still applies. If it does not, name the exact point where the reasoning breaks.

Study route

Save one clean worked answer from this lesson. Include the problem statement, notation, working, final answer and a short note explaining the computing meaning of the result.

Next lesson: Floating Point Representation and Approximation Error.