Arithmetic Overflow and Underflowing

30.8k Views Asked by At

I am a bit unclear about underflowing in terms of binary representation.

Let's say that an unsigned 8-bit variable gets overflown from the addition of $150+150$.

A signed 8-bit variable gets underflown after the subtraction of $-120-60$.

Now my point is let's think of 8-bit variable, we are subtracting $110-10$. Now let's convert this into an addition, $110+(-10)$. Since $-10$ is $11110110$ and $110$ is $01101110$. If we add these two binary numbers we will have a value after 8th bit to carry, which is I believe an overflown, however the final binary number is equal to $100$ and that's what we want and in terms of decimal value we did not lose anything. In that case do we have a overflow or underflow here?

2

There are 2 best solutions below

0
On

You don't have an overflow here: the result will be 01100100. Since the top bit indicates the sign, the addition process is not the same as for unsigned integers.

For example, 01100100+01100100 overflows for signed integers, because we can't carry from the 7th bit into 8th: the 8th bit is the sign. Over unsigned integers, there is no overflow.

Conversely, 01100100+11110110 overflows for unsigned integers, but not for signed ones.

My point is: the process of adding of numbers represented by binary strings depends on what representation is used. (You would not add floating point representations bitwise...)

0
On

The term arithmetic underflow (or "floating point underflow", or just "underflow") is a condition in a computer program where the result of a calculation is a number of smaller absolute value than the computer can actually store in memory.