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