difference between JavaScript bit-wise operator code and Python bit-wise operator code

后端 未结 2 599
余生分开走
余生分开走 2021-01-13 05:44

I have converted JavaScript code which uses bit-wise operators in that code to Python code, but there is one problem when i do this in JavaScript and Python

         


        
相关标签:
2条回答
  • 2021-01-13 06:25

    As stated in this SO answer, in javascript the bitwise operators and shift operators operate on 32-bit ints, and your second example overflows the 32 bit capacity, so the python equivalent would be:

    (424970184 << 10) & 0x7FFFFFFF
    

    (you get a "modulo"/"masked" value with the signed 32 bit integer mask, not the actual value)

    In Python there's no limit in capacity for integers, so you get the actual value.

    0 讨论(0)
  • 2021-01-13 06:41

    If you want the JavaScript equivalent value then what you can do is :

    import ctypes
    
    print(ctypes.c_int(424970184 << 10 ^ 0).value)
    

    Output:

    1377771520
    
    0 讨论(0)
提交回复
热议问题