python 中的零填充右移
Zero fill right shift in python
function(e, t) {
return e << t | e >>> 32 - t
}
我在js中有这个方法,我对shift操作不是很了解。我想把它写在 python 中。我如何在 python 中编写等效代码,因为它不支持 JS >>>
.
中的 零填充右移运算符
Python中没有内置零填充右移运算符,但您可以轻松定义自己的zero_fill_right_shift
函数:
def zero_fill_right_shift(val, n):
return (val >> n) if val >= 0 else ((val + 0x100000000) >> n)
然后你可以定义你的函数:
def f(e, t):
return e << t or zero_fill_right_shift(e, 32 - t)
function(e, t) {
return e << t | e >>> 32 - t
}
我在js中有这个方法,我对shift操作不是很了解。我想把它写在 python 中。我如何在 python 中编写等效代码,因为它不支持 JS >>>
.
Python中没有内置零填充右移运算符,但您可以轻松定义自己的zero_fill_right_shift
函数:
def zero_fill_right_shift(val, n):
return (val >> n) if val >= 0 else ((val + 0x100000000) >> n)
然后你可以定义你的函数:
def f(e, t):
return e << t or zero_fill_right_shift(e, 32 - t)