The judgment of unsigned integer overflow is simple.
When a signed integer overflows, there are only two situations: positive overflow and negative overflow, that is, two positive numbers are added to get a negative number or two negative numbers are added to get a positive number.
It should be noted that
This method can't judge overflow, because whether x+y overflows or not, x+y-y is always equal to x, because the complement is added to form an Abelian group, and Abelian groups have (x+y)-x = = y.
Subtraction overflow
Subtracting a number can be understood as adding the negative number of this number, so
However, there are some problems in this writing. TMIN (that is, the minimum value of the value range, which is -2 147483648 at 32 bits) is not considered, and TMIN has no inverse number, so it is necessary to consider the case that y is TMIN. In fact, when y = = tmin, X-y will overflow only when x> =0.
Multiplication overflow