char c1 = 255;
char c2 = 255;
char result = c1 + c2;
Testing for integer overflow
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
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.
Quote:Original post by SiCraneIn fact checking if it's smaller than one of them is enough.
Try checking if the result is less than the addends.
if(c1 += c2, c1 < c2) { ..overflow.. }
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.
I don't know of any fully general non-annoying way to test integer overflow that takes into account signed types.
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.
[Edited by - iMalc on August 27, 2005 3:19:07 AM]
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]
Nope. Consider the 8-bit signed addition of 5 plus 125 as translated into unsigned 8-bit addition:
However the maximum for signed 8-bit two's complement is 127 so additionWouldOverflow should have returned true.
(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.
You might consider using assembly to add the two and check the overflow bit, not machine independant, but you will know for sure.
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:
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.
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
Popular Topics
Advertisement