Skip to content

Overflows And Underflows

Overflow / Underflow Detection in Two's Complement

+ve + +ve = -ve => Overflow Detected

-ve + -ve = +ve => Undeflow Detected

During assessments, an underflow may be referred to as overflow as well.

But what about one positive and one negative integer? These cannot give any form of overflow, as the range of their addition cannot exceed beyond the domain (they're opposing each other).

We can use this to define the overflow check. Consider \(a_k\) and \(b_k\) as MSBs, and \(s_{k-1}\) and \(s_k\) as carry overs before and after their addition. An overflow is observed if,

Expressions to check for Overflow / Underflow
  • \(C_i \oplus C_j\)
  • \(a_{k-1} * b_{k-1} * \overline{s_{k-1}} + \overline{a_{k-1}} * \overline{b_{k-1}} * s_{k-1}\)

What if Overflow?

  • Raise an error or a flag. The user can handle this however they want. The FLAGS register in RISC langauges that we'll work in does this.
  • Wrap around. This allows the number to 'fit' within the required space, but can lead to unexpected behaviour. This sounds bad, but Java does this.
  • Cast to a "larger" variable type. Python does this.
  • Do whatever the hell you want! It's not your responsibility if there's an overflow, it's the programmer's. C does this.