Ch 2. Internal Data Representation of Characters, Integers, Real Numbers
2.4 Data Underflow, Overflow, and Interpreting Memory Content
As you’ve seen with integers and real numbers, digital systems need a fixed number of bits to store numbers. In the case of real numbers, their values are approximated, so arithmetic operations involving them are not always accurate. These situations, when the values are outside their expected range, are known as data underflow and overflow. Understanding these conditions is essential for writing reliable software and interpreting how data is stored in memory.
Overflow occurs when the result of a computation is above the maximum value that can be represented with the given number of bits.
Example 2.9
An unsigned 4-bit integer has a maximum value of [latex]1111_{2} = 15_{10}[/latex]. If we add 1 to 15, we get
1111 (15)
+ 0001 (1)
-------
1 0000
The result is 5 bits, but we only have 4 bits to store the result. The leftmost bit is discarded, and the result is [latex]0000_{2} = 0_{10}[/latex].
Example 2.10
A signed 4-bit two’s complement has a maximum value of [latex]0111_{2} = 7_{10}[/latex]. If we add 1 to 7, we get
0111 (+7)
+ 0001 (+1)
-------
1000
Overflow can lead to incorrect results and unexpected behaviour in programs, especially in loops, counters, and arithmetic operations.
Underflow occurs when a number is too small (in magnitude) to be represented in the chosen format, especially in floating-point arithmetic.
Example 2.11
In IEEE 754 single-precision, the smallest positive normalized number is approximately [latex]1.18 \times 10^{-38}[/latex]. If a calculation results in a number smaller than [latex]1.18 \times 10^{-38}[/latex], it cannot be represented as a normalized number.
Instead, IEEE 754 uses denormalized numbers (also called subnormal numbers) to fill the gap between the smallest normalized number and zero.
Denormalized numbers work by setting all exponent bits to 0s
00000000
which is interpreted as -126, but no leading 1. The mantissa represents a fraction less than 1.0. The smallest positive denormalized number has a mantissa where only the last bit is 1
00000000000000000000001
and its value is [latex]2^{-126} \times 2^{-23} = 2^{-149} \approx 1.4 \times 10^{-45}[/latex].
Underflow is common in scientific computing and can lead to loss of significance or precision in calculations.
You need to understand how data is stored in memory, as this is crucial for debugging and low-level programming. When working with a sequence of bits in memory, keep in mind that, depending on the context, they can represent different types of data.
Example 2.12
The same 32-bit pattern can represent a large integer or a small floating-point number. Consider
01000001 00000000 00000000 00000000
This can represent the unsigned integer 1,090,519,040, the signed integer 1,090,519,040, the IEEE 754 float ≈ 8.0 x 10-34, or the character array ‘A’ followed by 3 nulls.
Example 2.13
A byte can represent different things. Consider
01000001
This can represent ‘A’ in ASCII, ‘A’ in UTF-8 Unicode, part of a multibyte character in UTF-16, or the integer 65.
Examples 2.14
The order in which bytes are stored affects how multi-byte values are interpreted. Consider storing the 32-bit hex value
0x12345678
If the byte order is stored from left to right (big-endian) in the memory layout 12 34 56 78, and interpreted as a 32-bit integer, it would be 305419896. On the other hand, if the byte order is stored right-to-left (little-endian) in the memory layout 78 56 34 12 and interpreted as a 32-bit integer, it would be 2018915346.
Real-World Example 2.4: Credit Card Transaction System
A financial system processes millions of credit card transactions per day. Each transaction amount is stored in a fixed-size memory field, either as a floating-point or an integer value. Let’s say the fixed size is 32-bit for signed integers. This means that the largest dollar amount of money in the account can be [latex]2^{31} – 1 = $2,147,483,647[/latex].
Now consider an account from a high-volume merchant with a current balance of $2,147,483,000 that receives a $1,000 deposit. This total exceeds the largest dollar amount:
Current total: 2,147,483,000
New transaction: +1,000
New total: 2,147,484,000 [latex]\rightarrow[/latex] OVERFLOW!
In fact, the result wraps around to the negative number -$2,147,483,296. The merchant would not be happy!