如何在python中获得逻辑右移二进制移位 [英] How to get the logical right binary shift in python

查看:67
本文介绍了如何在python中获得逻辑右移二进制移位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如标题所示,在JavaScript中有一个特定的运算符>>> .例如,在JavaScript中,我们将得到以下结果:

As revealed by the title, in JavaScript there is a specific operator >>>. For example, in JavaScript we will have the following result:

(-1000) >>> 3 = 536870787

(-1000) >> 3 = -125

1000 >>> 3 = 125

1000 >> 3 = 125

那么是否存在某种表示此>>> 的方法或运算符?

So is there a certain method or operator representing this >>>?

推荐答案

对此没有内置的运算符,但是您可以自己轻松地模拟>>> :

There isn't a built-in operator for this, but you can easily simulate the >>> yourself:

>>> def rshift(val, n): return val>>n if val >= 0 else (val+0x100000000)>>n
... 
>>> rshift(-1000, 3)
536870787
>>> rshift(1000, 3)
125

以下替代实现无需使用 if :

The following alternative implementation removes the need for the if:

>>> def rshift(val, n): return (val % 0x100000000) >> n

这篇关于如何在python中获得逻辑右移二进制移位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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