python中的零填充右移 [英] Zero fill right shift in python
本文介绍了python中的零填充右移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
function(e, t) {
return e << t | e >>> 32 - t
}
我在js中有此方法,我对移位操作不甚了解.我想用python编写.如何使用python写等效代码,因为它不像JS中那样支持零填充右移运算符 >>>
.
I have this method in js, I do not understand in deep about shift operation. I want to write that in python. How can I write the equivalent code in python as it does not support Zero Fill Right Shift Operator as in JS >>>
.
推荐答案
Python中没有内置的零填充右移运算符,但是您可以轻松定义自己的zero_fill_right_shift
函数:
There is not a built-in zero fill right shift operator in Python, but you can easily define your own zero_fill_right_shift
function:
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)
这篇关于python中的零填充右移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文