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

M A SIDDIQUI
  • 2,028
  • 1
  • 19
  • 24
  • 1
    does my answer solve your problem? – lmiguelvargasf Mar 06 '19 at 03:18
  • Duplicate of: [How to get the logical right binary shift in python](https://stackoverflow.com/questions/5832982/how-to-get-the-logical-right-binary-shift-in-python) An elegant solution is: `def rshift(val, n): return (val % 0x100000000) >> n`. See that question and answer. – Ali Apr 22 '20 at 16:50

1 Answers1

3

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)
lmiguelvargasf
  • 63,191
  • 45
  • 217
  • 228