Each integer number used in digital hardware is represented in binary form using a fixed number of bits (memory space for storing the number). Due to the fixed number of bits, there is a limited number of unique values that can be represented.
Fig. 1. Storage size and unique values
The relation between the range and the storage size, that can be defined as range = 2^n, where n is number of bits .
Overflow occurs when the result from a performed operation is a number larger than what can fit in the memory storage space dedicated to it. So even though operations are performed on valid values, the result of the operation may not be valid.
Overflow definition is dependent on:
-
- binary number representation system (unsigned , signed etc)
-
- operation being performed (subtraction, adding etc.)
- number of bits used (storage size)
Overflow detection in different binary number representations
Two’s complement
- Adding – When two signed 2’s complement numbers are added, overflow is detected if:
- both operands are positive and the result is negative
- both operands are negative and the result is positive.
- Overflow never occurs when adding operands with different signs
- Subtraction – When two s igned 2’s Complement numbers are subtracted, overflow occurs only if:
- the operands have different signs and the result has the same sign as the subtrahend.
Unsigned
- Adding – When two unsigned numbers are added, overflow occurrs if
- there is a carry out of the leftmost bit.
- Subtraction
- No overflow is expected to occur. We always subtract the smaller value from the bigger value.
- Expectation is that the subtrahend is always bigger than the minuend. If this is not the case then signed representation should be used for storing the result. Detection of overflow is not possible, because the there is a wrap around.
Dealing with overflow
Saturation – If the result of an operation is greater than the maximum, it is set (“clamped”) to the maximum; if it is below the minimum, it is clamped to the minimum. The name comes from how the value becomes “saturated” once it reaches the extreme values; further additions to a maximum or subtractions from a minimum will not change the result.
Wrap Around
Leave A Comment