9

I find using 0xFFFFFFFF masking to detect int32 overflow in Python may work for positive numbers.

The expression:

x & 0xFFFFFFFF == x

will return True if x doesn't oveflow and x is larger than 0.

However, this expression doesn't work for negative integers, for example:

(-7 & 0xFFFFFFFF) == -7

will return False, though -7 shouldn't overflow the int32 ranges..

Does anyone have ideas about why this approach doesn't work for -7 and how to make it work?

Hanfei Sun
  • 45,281
  • 39
  • 129
  • 237

1 Answers1

6

That's because Python doesn't consider any fixed width for numbers. So you don't have any sign bit like that we have for C/C++ languages (The most significant bit). In the other words, when you do a bit-wise AND between the negative number and 0xffff, the result is a big positive number instead of the negative number:

>>> print(-7 & 0xFFFF)
65529
>>> print(-7 & 0xFFFFFFFF)
4294967289
>>> 

A confirmation for above claim:

>>> x = -1
>>> y = -2
>>> z = -4
>>> x.bit_length()
1
>>> y.bit_length()
2
>>> z.bit_length()
3
>>> 

While in C/C++ languages as we have a fixed width for numbers:

#include <iostream>
#include <string>

int main()
{
  int i = -7 & 0xFFFFFFFF;
  std::cout <<  i;
}

the output is the same negative number (If we choose correct length for right side of & operator):

-7 

I guess you need to define a function to aim your goal and passing the number with the length.(4 byte or 8 byte for example).

Something like this:

>>> def isOverflow(num, width=32):
    if num > 0 and num > 2**(width-1) -1 :
        return True
    elif num < 0 and abs(num) > 2**(width-1):
        return True
    return False

Or the more efficient version:

def isOverflow(num, width=32):
    if num > 0:
        if num >> width-1:
            return True
    elif num < 0:
        if abs(num) > (1 << width - 1):
            return True
    return False

That works as below:

>>> ================================ RESTART ================================
>>> 
>>> isOverflow(-129,8)
True
>>> isOverflow(-128,8)
False
>>> isOverflow(128,8)
True
>>> isOverflow(127,8)
False
>>> isOverflow(0x7fffffff)
False
>>> isOverflow(0x8fffffff)
True
EbraHim
  • 2,279
  • 2
  • 16
  • 28