在Python中获得与Java相同的左移 [英] Get the same shift left in Python as Java

查看:141
本文介绍了在Python中获得与Java相同的左移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

具体地说,我想使用这个号码:

Specifically I want to take this number:

x = 1452610545672622396

并执行

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

我想获得: -6403331237455490756,这是我在Java中获得的东西

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

代替:12043412836254060860,这是我在Python中获得的(这是我不想要的)

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

在Java中,我这样做:

In Java I do:

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

推荐答案

您可以使用

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__

这篇关于在Python中获得与Java相同的左移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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