Python Glossary Русский

all Python 3operatorfunction

operator.lshift

bitwise

Left shift: a << b (multiplication by 2**b).

Syntax

operator.lshift(a, b)

Python integers never overflow: lshift(1, 100) honestly yields a 31-digit number, not zero as in fixed-width languages. A negative shift raises ValueError instead of shifting the other way. Shifts suit bit masks (1 << n), but to multiply by a power of two a * 2**n reads more clearly.

Examples

import operator
print(operator.lshift(1, 3))   # → 8
print(operator.lshift(3, 2) == 3 << 2)   # → True
print(operator.lshift(5, 1) == 5 * 2)   # → True
print(list(map(operator.lshift, [1, 2, 3], [4, 1, 0])))   # → [16, 4, 3]
print(operator.lshift(1, -1))   # → ValueError

See also

Open in the full glossarydocs.python.orgReport an inaccuracy