Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zero fill right shift in python

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

like image 906
M A SIDDIQUI Avatar asked Jul 30 '26 13:07

M A SIDDIQUI


1 Answers

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 | zero_fill_right_shift(e, 32 - t)
like image 110
lmiguelvargasf Avatar answered Aug 02 '26 02:08

lmiguelvargasf



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!