Edexcel International Advanced Level Computer Science, point 2.2.3
Definition
A computer stores each number in a fixed number of bits, so there is a largest and smallest value it can hold.
- Overflow: a result is too large to be stored in the bits available: bigger than the largest value, or, for signed numbers, more negative than the most negative value.
- Underflow: a result is too small to be stored: it is closer to zero than the smallest step the format can show, so it can't be represented accurately and may be stored as 0.
The bits that don't fit are lost, so the stored result is wrong or inaccurate.
Function and why it matters
Overflow and underflow aren't something we want, but we must understand them because they cause wrong answers, crashes and security problems. The processor can detect them using flags in the status register (1.1.4), so the program can respond.
Characteristics
Unsigned numbers
For bits, the range is to (8 bits: 0 to 255).
- Overflow happens when the result is more than 255. It shows as a carry out of the most significant bit.
The 8 bits hold 00101100 , which is . Wrong, and the carry flag is set.
Signed (two's complement) numbers
For 8 bits the range is to .
- Overflow with two positive numbers: the result is too big, and comes out negative.
- Overflow with two negative numbers: the result is too negative, and comes out positive.
How to detect it in signed arithmetic
Overflow has occurred if:
- both operands have the same sign, but the result has a different sign.
(Adding numbers with different signs can never overflow.) Hardware does the same thing by comparing the carry into the sign bit with the carry out of it: if they differ, the overflow flag is set.
| Case | Unsigned | Signed (two's complement) |
|---|---|---|
| How it shows | Carry out of the MSB | Result has the wrong sign |
| Flag | Carry | Overflow |
| Example (8 bits) | 200 + 100 | 100 + 50, or −100 + −50 |
Underflow
Underflow happens when a result is smaller than the smallest value the format can represent, for example in fixed point (2.1.4), when:
- a small number is divided by a large one, or
- the bits of a small number are shifted right, and the 1s fall off the end.
With 5 whole bits and 3 fraction bits, the smallest step is . The number 0.25 is 00000.010. Shifting it right by 2 should divide it by 4 to give 0.0625, but that is smaller than 0.125, so the 1 falls off the end and the result is 0:
The answer 0.0625 can't be stored, so it becomes 0: the information is lost.
Operation: what the program sees
Without checks, the program continues with the wrong value. For example, a counter that reaches 255 and then wraps back to 0, or a bank balance that suddenly becomes negative.
Some languages (such as Python) have integers that grow as needed, so they don't overflow, but fixed-width types, hardware registers and many libraries do.
Consequences
- Wrong results, often with no warning.
- Crashes or infinite loops (a loop counter that wraps round).
- Security flaws: an integer overflow can make a program allocate too little memory, which lets an attacker write past the end of a buffer.
- Failures in real systems: the Ariane 5 rocket (1996) was lost when converting a 64-bit value to a 16-bit integer overflowed.
How to prevent or handle it
- Use more bits (a wider data type) when the result might be large.
- Check the range before the calculation, or test the overflow and carry flags after.
- Raise an error or exception if it happens.
- Saturating arithmetic: stick at the largest or smallest value instead of wrapping (used in audio and graphics).
- Use a type with arbitrary-precision integers where needed.
- For underflow: use more fraction bits (finer precision), or scale the values so results stay above the smallest step.
- Test with extreme values (the largest, the smallest and values just either side).
Worked example
Task: a game stores a player's health in an 8-bit unsigned variable. It is currently 240, and the player picks up a +30 potion. (a) What does the variable hold afterwards? (b) How would you fix it?
(a)
- , which is more than 255, so overflow occurs.
- In binary:
11110000and00011110. The sum is100001110, which is 9 bits. - The lowest 8 bits are kept:
00001110. - The health drops from 240 to 14, a very nasty surprise for the player, and the carry flag is set.
(b) Options:
- Cap the value (saturate): if the total would be more than 255, store 255.
- Check the flag or the range first, and refuse or limit the pickup.
- Use a 16-bit variable if larger values are valid.
Exam tip: be clear that overflow is a result too large to be stored and underflow is a result too small (too close to zero) to be represented accurately, for example after dividing a small number by a large one, or a right shift. Give the evidence of it: a carry out for unsigned, a sign change for signed. When asked about consequences, say an incorrect result that may not be noticed. When asked how to prevent it, say use more bits or check first.
Check yourself
What is overflow?
When the result of a calculation is too large to be stored in the number of bits available, so some bits are lost and the stored result is wrong.
What is the largest unsigned value in 10 bits?
1023 (2¹⁰ − 1).
How can you tell that overflow has occurred when adding two positive two's complement numbers?
The result has a negative sign bit (the MSB is 1).
What is underflow, and when can it happen?
A result too small (too close to zero) to be represented with the bits available, so it is stored inaccurately or as 0. For example, dividing a small fixed-point number by a large one, or shifting it right.
Give two ways of preventing overflow errors
Any two of: use more bits; check the range before or the flags after; raise an error; use saturating arithmetic; use arbitrary-precision integers.
What is 8-bit unsigned 250 + 10 stored as, and what is the correct answer?
It is stored as 4 (the carry is lost), and the correct answer is 260.