Testing for integer overflow

Started by
27 comments, last by Jan Wassenberg 18 years, 7 months ago
Is there any fast and fairly machine independant methods of testing to see if adding to integers together would result in an overflow (going beyond the bit limit). example

char c1 = 255;
char c2 = 255;

char result = c1 + c2;

Result is going to result in 254, since the char can't hold it resulting value. Now I will say, I'm not also don't want to use a bigger sized variable to catch the overflow, since in the code I'm writting I will be using the largest primitive variables available.

"I can't believe I'm defending logic to a turing machine." - Kent Woolworth [Other Space]

Advertisement
Try checking if the result is less than the addends.
Quote:Original post by SiCrane
Try checking if the result is less than the addends.
In fact checking if it's smaller than one of them is enough.
if(c1 += c2, c1 < c2) { ..overflow.. }
Thanks for the fast responses.

"I can't believe I'm defending logic to a turing machine." - Kent Woolworth [Other Space]

Be careful, that trick only works for unsigned types. And char is usually signed by default. If you turn up the warning level for your compiler it will probably complain about assigning 255 to a char.

I don't know of any fully general non-annoying way to test integer overflow that takes into account signed types.
-Mike
With twos-complment, to do the overflow check for signed types, you can simply cast them to the unsigned type (of the same size) first.
bool additionWouldOverFlow(unsigned char a, unsigned char b){    return (a + b) < b;}bool additionWouldOverFlow(signed char a, signed char b){    return additionWouldOverFlow((unsigned char)a, (unsigned char)b);}
Edit: It seems that my comment above was incorrect. My appologies.

[Edited by - iMalc on August 27, 2005 3:19:07 AM]
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
Nope. Consider the 8-bit signed addition of 5 plus 125 as translated into unsigned 8-bit addition:
(a + b) < b(5 + 125) < 125130 < 125           (we get 130 instead of -126 because of the unsigned conversion)== false

However the maximum for signed 8-bit two's complement is 127 so additionWouldOverflow should have returned true.
-Mike
You might consider using assembly to add the two and check the overflow bit, not machine independant, but you will know for sure.
"Think you Disco Duck, think!" Professor Farnsworth
The result of your original code is -2, well within the range of chars.
Types shorter than int are always promoted to int before performing arithmetic operations -- that's one of what the standard calls the usual arithmetic conversions. So for testing overflow of those types, it suffices to test if the result is out of the range of the intended target type. For example, ((unsigned char) 255) + ((unsigned char) 255) produces 510. (Obviously, you can only do that before you put the result back into a char variable.)

In the event that you doubt that chars are promoted to ints, see what the following gets you:
std::cout << ('a' + 'b');


For int, the result is never automatically promoted. As already noted, for unsigned ints it suffices to determine if the result is less than either of the operands. For signed ints, it's a little more tricky.

bool would_overflow (signed char a, signed char b){  signed char c = a;  if (a > 0 && b > 0)    // The sum of positive numbers can't be less than any of the numbers.    return (c += b) < a;  else if (a < 0 && b < 0)    // The sum of negative numbers can't be greater than any of the numbers.    return (c += b) > a;  else    // A negative and positive addition cannot overflow.    return false;}

This topic is closed to new replies.

Advertisement