Get the same shift left in Python as Java

心已入冬 提交于 2019-12-12 18:09:34

问题


Specifically I want to take this number:

x = 1452610545672622396

and perform

x ^= (x << 21) // In Python I do x ^= (x << 21) & 0xffffffffffffffff

I want to get: -6403331237455490756, which is what I get in Java

instead of: 12043412836254060860, which is what I get in Python (which is what I don't want)

EDIT: In Java I do:

long x = 1452610545672622396;
x ^= (x << 21);

回答1:


You can use 64bit signed int like java using ctypes.c_longlong, please see example below:

from ctypes import c_longlong as ll

x = 1452610545672622396

output = ll(x^(x<<21))

print output
print output.__class__



回答2:


You might cause an overflow. Java long is 64 bit long while python has no size limit. Try using Long wrapper class of long. The Long object has also no limits (Well technicially everything has its limits...).



来源:https://stackoverflow.com/questions/38872627/get-the-same-shift-left-in-python-as-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!