1

In javascript:

var zx=1566202383
var xz=1
document.write(zx << xz | zx >>> 32 - xz)

result is -1162562530

In python (i used - How to get the logical right binary shift in python):

zx=1566202383
xz=1
def rshift(val, n): 
    return val>>n if val >= 0 else (val+0x100000000)>>n

print (zx << xz | rshift(zx, 32 - xz)) 

result is: 3132404766

So, how to get the same result in python, like in javascript?

basement
  • 19
  • 3
  • In JavaScript bitwise operations are ***always*** performed on 32-bit integers - anything that's bigger will be forcefully truncated to 32 bits. `2 ** 31 - 1 | 0 = 2147483647` but `2 ** 31 | 0 = -2147483648` – VLAZ Nov 30 '20 at 18:07
  • See: [difference between JavaScript bit-wise operator code and Python bit-wise operator code](https://stackoverflow.com/q/41610186) | [Replicating Javascript bitwise operation in Python](https://stackoverflow.com/q/17102767) | [different results between javascript and python when using bitwise operators](https://stackoverflow.com/q/53567349) | [Javascript bitwise operator “<<”, “>>>” to Python](https://stackoverflow.com/q/20028735) | [JavaScript and Python bitwise OR operation gave different results](https://stackoverflow.com/q/60387008) – VLAZ Nov 30 '20 at 18:10
  • thanks @VLAZ, i used- import ctypes and result is correct. – basement Nov 30 '20 at 18:32

0 Answers0