Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect int32 overflow using 0xFFFFFFFF masking in Python?

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?

like image 598
Hanfei Sun Avatar asked Apr 24 '16 06:04

Hanfei Sun


People also ask

What does 0xFFFFFFFF mean in Python?

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.

How does Python store negative integers?

Negative numbers are written with a leading one instead of a leading zero. So if you are using only 8 bits for your twos-complement numbers, then you treat patterns from "00000000" to "01111111" as the whole numbers from 0 to 127, and reserve "1xxxxxxx" for writing negative numbers.


1 Answers

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
like image 139
EbraHim Avatar answered Sep 30 '22 08:09

EbraHim