all Python 3operatorfunction
operator.rshift
bitwise
Right shift: a >> b (floor division by 2**b).
Syntax
operator.rshift(a, b)
For negative numbers the shift rounds down, towards minus infinity: rshift(-5, 1) gives -3, not -2. This matches floordiv: a >> n equals a // 2**n. A negative shift count raises ValueError. To read a single bit, combine the shift with a mask: (a >> k) & 1.
Examples
import operator
print(operator.rshift(16, 2)) # → 4
print(operator.rshift(16, 2) == 16 >> 2) # → True
print(list(map(operator.rshift, [8, 16, 32], [1, 2, 3]))) # → [4, 4, 4]
print(operator.rshift(-9, 1) == -9 // 2) # → True
print(operator.rshift(1, -1)) # → ValueError
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy