0

What is a good way of anticipating if overflow will happen or not, before computing.

Suppose I am using Python, but wanted to know if overflow would have happened if you ran it in another language with 32bits int. How should I do that? Should I look at the number of bits?

user25004
  • 1,868
  • 1
  • 22
  • 47
  • Are you incrementing counters, doing simple arithmetic, doing bit manipulation, adding values from recursive calls ... ? I guess it depends on what you're doing; e.g. if x > (MAX / y) then don't do x*y. – m69's been on strike for years Sep 20 '17 at 20:06
  • I am negating the number. But regardless, what are conditions on the result to reflect overflow. – user25004 Sep 20 '17 at 20:12
  • There's only one number in the 32-bit signed integer range that you can't correctly negate, and that's -2^31. Or do you mean something different? – m69's been on strike for years Sep 20 '17 at 20:23
  • This seems to have Python-specific information: https://stackoverflow.com/questions/37135106/what-is-good-way-to-negate-an-integer-in-binary-operation-in-python – m69's been on strike for years Sep 20 '17 at 20:26
  • 1
    @user25004 see [Cant make value propagate through carry](https://stackoverflow.com/a/26603589/2521214) and look how the carry flag is computed ... Also this [How to deal with overflow and underflow?](https://stackoverflow.com/a/33006665/2521214) might interest you. – Spektre Sep 21 '17 at 06:55

0 Answers0