Home > database >  Different calculation result [duplicate]
Different calculation result [duplicate]

Time:10-01

Explain to me why the result of the following calculation is different:

Python:

tmp = 4235635936 << 0 #4235635936

JS

let tmp = 4235635936 << 0 // -59331360

Question - how do I get the same result in python as in js ?

CodePudding user response:

The binary representation for both numbers is the same. One is signed, the other is unsigned.

>>> struct.pack("<i", -59331360)
b'\xe0\xacv\xfc'
>>> struct.pack("<I", 4235635936)
b'\xe0\xacv\xfc'
>>>

CodePudding user response:

in JS, the bitwise operators and shift operators operate on 32-bit ints, and your example overflows the 32-bit capacity.

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

  • Related