Search code examples
pythonbit-manipulationoperator-keywordxor

Bitwise XOR operator not working in python


I am trying to do this :

print 17593028247552 ^ 909522486

The result must be 67450422(as is in javascript) but I am getting 17592253494838L

Please help me ! Thanks in advance :)


Solution

  • Python 2's long type (or int in Python 3) can grow as large as they need to, but it looks like you want a 32 bit result.

    You just need to mask the result so you only get the low 32 bits of the result. And I guess that since you're on Python 2 you should also convert it from long to int.

    >>> int((17593028247552 ^ 909522486 ) & 0xffffffff)
    67450422