bitwise operators - How to get the logical right binary shift in python -


as revealed title. in javascript there specific operator '>>>'. example, in javascript have following result:

(-1000) >>> 3 = 536870787

(-1000) >> 3 = -125

1000 >>> 3 = 125

1000 >> 3 = 125

so there method or operator representing '>>>'?

there isn't built-in operator this, can simulate >>> yourself:

>>> def rshift(val, n): return val>>n if val >= 0 else (val+0x100000000)>>n ...  >>> rshift(-1000, 3) 536870787 >>> rshift(1000, 3) 125 

the following alternative implementation removes need if:

>>> def rshift(val, n): return (val % 0x100000000) >> n 

Comments