在JS和Python ^运营商之间的区别 [英] Difference between ^ Operator in JS and Python

查看:273
本文介绍了在JS和Python ^运营商之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要移植一些JS code涉及的Math.random()* 2147483648)^(新日).getTime()。虽然它看起来像小数字,蟒蛇功能和JS功能在功能上等同,但有大量这样的,价值最终完全不同。

I need to port some JS code which involves Math.random()*2147483648)^(new Date).getTime(). While it looks like for smaller numbers, the python function and the JS function are equivalent in function, but with large numbers like this, the values end up entirely different.

的Python:

>>> 2147483647 ^ 1257628307380
1257075044427

使用Javascript:

Javascript:

> 2147483647 ^ 1257628307380
-1350373301

我怎样才能从蟒蛇的JavaScript值?

How can I get the Javascript value from python?

推荐答案

Python有unlimited- precision整数,而JavaScript是使用32位整数。您可以手动应用32位的限制得到你想要的结果:

Python has unlimited-precision integers, while Javascript is using a 32-bit integer. You can manually apply a 32-bit limit to get the result you want:

def xor32bit(a, b):
    m = (a ^ b) % (2**32)
    if m > (2**16):
        m -= 2**32
    return m

这篇关于在JS和Python ^运营商之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆