Testing for integer overflow

This topic is 4661 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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.

Share on other sites
Try checking if the result is less than the addends.

Share on other sites
Quote:
 Original post by SiCraneTry 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.. }

Share on other sites
Thanks for the fast responses.

Share on other sites
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.

Share on other sites
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]

Share on other sites
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.

Share on other sites
You might consider using assembly to add the two and check the overflow bit, not machine independant, but you will know for sure.

Share on other sites
The result of your original code is -2, well within the range of chars.

Share on other sites
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;}

• 10
• 48
• 12
• 10
• 10
• Forum Statistics

• Total Topics
631383
• Total Posts
2999681
×