[Math] Arithmetic Overflow and Underflowing

binarycomputer-arithmetic

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?

Best Answer

Say you have $8$-bits signed integers. The range of representable integers start at $-128$ and ends at $127$.

If you perform $127+1$, you obtain $-128$ : $0111 1111+0000 0001 = 1000 0000$ and the overflow flag is turned on.

If you perform $-128-1$, you obtain $127$ : $1000 0000-0000 0001 = 0111 1111$ and the overflow flag is turned on.

In both cases, these are overflows.

Underflows refer to floating point underflow, where an operation result in a number that is too small to be representable. For example, if the exponent part can represent values from $-127$ to $127$, then any number with absolute value less than $2^{-127}$ may cause underflow.

Related Question