Zero fill right shift in python

怎甘沉沦 提交于 2019-12-20 05:42:40

问题


function(e, t) {
    return e << t | e >>> 32 - t
}

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 >>>.


回答1:


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)

Then you can define your function:

def f(e, t):
    return e << t or zero_fill_right_shift(e, 32 - t)


来源:https://stackoverflow.com/questions/55014710/zero-fill-right-shift-in-python

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