So I tried the following in the python shell:
>>> x = -1
>>> x < 0
True
>>> x = -x
>>> x ^= 0xffffffff
>>> x += 1
>>> x < 0
False
So if i have a number thats initially negative, and i negate it and take the twos compliment, shouldnt it be negative again? why does python think the number is positive?
You can check with your code using
Print
, You are making positive before that,For more details, Just visit here https://wiki.python.org/moin/BitwiseOperators