Computer ScienceChapter 1: Computer Systems
1.2.4 Overflow and Underflow
Overflow
Explanation level
In computers, overflow and underflow are problems that occur when calculations produce results that are too large or too small to be represented within the allowed number of bits. These conditions are important because they can cause errors in programs and lead to unexpected behavior or bugs. Overflow happens when a value is too large to fit in the available space, while underflow occurs when a value is too small to be represented properly. Understanding these issues is important to prevent problems in software. Overflow is being explained as follows at that level.
Definition
Overflow happens when a calculation produces a result that is larger than the maximum value that can be stored or represented with the available number of digits or bits in a system. This causes the system to be unable to properly show or handle the result.
Underflow happens when a calculation produces a result that is smaller than the minimum value that can be represented with the available number of bits. This is especially common with floating-point numbers, where a value becomes so small that it is treated as zero.
Main Idea
Every data type in a computer has a fixed number of bits. This fixed size determines the range of values that can be stored. If a calculation produces a result outside this range, the computer cannot represent it correctly. The result "wraps around" or becomes incorrect.
- Overflow → result is too large.
- Underflow → result is too small.
Core Concept
For an n-bit unsigned integer, the range of values that can be represented is:
0 to (2n - 1)
For an n-bit signed integer (using two's complement), the range is:
-2n-1 to (2n-1 - 1)
If a calculation produces a value outside this range, overflow (or underflow) occurs.
Detailed Explanation
Example: 8-bit Unsigned Integer Overflow
An 8-bit unsigned integer can represent values from 0 to 255. If a user attempts to add 1 to 255, the result is 256, which cannot be represented with just 8 bits.
11111111 (255 in binary)
+ 1
\hline 100000000 (256 in binary)
The 9th bit (the leading 1) is the overflow bit. Since only 8 bits are available, this bit is lost, and the result appears as:
00000000 (0 in decimal)
So 255 + 1 incorrectly gives 0 instead of 256. This is overflow.
Example: 8-bit Signed Integer Overflow
An 8-bit signed integer can represent values from -128 to 127. If we add 1 to 127:
01111111 (127 in binary)
+ 1
\hline 10000000 (-128 in two's complement)
The result is -128, not 128. This is also overflow—the correct answer 128 cannot be represented in 8-bit signed form.
Example: Underflow
Underflow typically occurs with floating-point numbers. For example, if the smallest positive number that can be represented is 1 × 10-38, and a calculation produces 1 × 10-40, the result is too small and is treated as 0. This is underflow.
Intermediate Level
Overflow and underflow are not just theoretical—they cause real bugs in programs.
Why does overflow happen? Because the number of bits is fixed. When the result of an operation requires more bits than are available, the extra bits are lost.
What happens to the extra bit? In unsigned integers, the carry-out from the most significant bit is discarded. The result wraps around to a small value.
What about signed integers? In signed integers, overflow occurs when the sign bit is affected. For example, adding two positive numbers gives a negative result, or adding two negative numbers gives a positive result. This is a clear sign of overflow.
- For unsigned addition: overflow occurs if there is a carry out of the MSB.
- For signed addition: overflow occurs if the carry into the MSB and the carry out of the MSB are different.
Important Points
- - Overflow = result too large to represent.
- - Underflow = result too small to represent.
- - Both occur because the number of bits is fixed.
- - 8-bit unsigned range: 0 to 255.
- - 8-bit signed range: -128 to 127.
- - 255 + 1 in 8-bit unsigned = 0 (overflow).
- - Overflow can cause programs to behave unexpectedly.
- - Floating-point numbers can also overflow (to infinity) and underflow (to zero).
Remember
- - Overflow → too big.
- - Underflow → too small.
- - Fixed bits → fixed range.
- - 8-bit unsigned: 0–255.
- - 8-bit signed: -128–127.
- - 255 + 1 = 0 in 8-bit unsigned (overflow).
- - Carry out of MSB indicates unsigned overflow.
Common Mistakes
- - Thinking that 255 + 1 = 256 in an 8-bit system. It actually gives 0.
- - Confusing overflow with underflow.
- - Forgetting that signed and unsigned integers have different ranges.
- - Assuming that overflow always causes an error message. In many languages, it wraps around silently.
- - Thinking that underflow only happens with integers. It is more common with floating-point numbers.
- - Ignoring the sign bit when checking for signed overflow.
Quick Revision
- Overflow occurs when a result is too large for the available bits.
- Underflow occurs when a result is too small.
- 8-bit unsigned range: 0 to 255.
- 8-bit signed range: -128 to 127.
- Adding 1 to 255 in 8-bit unsigned gives 0.
- Overflow can cause incorrect results and bugs.
- Floating-point numbers can overflow to infinity and underflow to zero.
60-Second Revision
- Overflow = result too large.
- Underflow = result too small.
- Fixed bits → fixed range.
- 8-bit unsigned: 0–255; 8-bit signed: -128–127.
- 255 + 1 = 0 in 8-bit unsigned.
- Warning: Overflow often wraps around silently; always check ranges.
Did you understand this topic?
Your answer is saved with your learning record. It does not mark the topic as mastered.
Quick revision
Revision Zone
Review the key facts, formulas and checks before you move on.1.2.4 Overflow and Underflow
Sticky Notes · Quick Revision
Key Point 1
- Overflow = too large.
Key Point 2
- Underflow = too small.
Key Point 3
- 8-bit unsigned: 0–255.
Key Point 4
- 8-bit signed: -128–127.
Key Point 5
- 255 + 1 = 0 (overflow).
Key Point 6
- Carry out of MSB = unsigned overflow.
Key Point 7
- Sign change in signed addition = overflow.
Same supplied content as this Notes page (shared canonical source).