Search code examples
pythonpython-3.xbit-manipulationbitwise-operators

Bitwise shift returns unexpected value in Python


I'm performing 55 << 30 in C# and Python 3. C# returns -1073741824, which is what I want, but Python returns 59055800320. Can someone explain why exactly this happens, and what I can do to fix it?


Solution

  • python ints are arbitrarily large ... you can force it sort of

    import numpy
    
    numpy.int32(55) << 30