<<
Shift bits left — arithmetic doubling per step, with no 32/64-bit ceiling.
Common call
1 << n
Returns
a * 2**n as an exact int
Replaces
1 << 40 is 1099511627776 — no truncation, unlike JS/C
Watch out
negative shift counts raise ValueError
aa — The value to shift.type: int · required << nn — How many places — must be non-negative.type: int · required
→ int
Demo
Live evaluation
Try:
Inputs
aintvalue
nintplaces
Output
1 << 4
16
Each place doubles the value. The 40-place case matters: Python gives the exact 1099511627776 where fixed-width languages truncate or overflow. Negative counts raise Python’s exact ValueError.
Operands
| Name | Type | Required | Description |
|---|---|---|---|
| a | int | yes | The value to shift. |
| n | int | yes | How many places — must be non-negative. |
Return value
int — a with its bits moved n places left — exactly a * 2**n, at any size (Python ints never overflow).
Common patterns
Single-bit flags
Enumerate powers of two readably.
READ = 1 << 0 WRITE = 1 << 1 EXECUTE = 1 << 2
Fast powers of two
1 << n beats 2 ** n in hot loops.
size = 1 << exponent
Examples
1. Basic shift
1 << 4
Returns
162. Doubling
5 << 1
Returns
103. No 32-bit ceiling
1 << 40
Returns
1099511627776Pitfalls
1. Negative shift counts raise
Shifting by a negative amount is not a right shift.
Raises
1 << -1
ValueError: negative shift count
Fix
1 >> 1
use the other operator
2. Precedence below + and -
1 << 2 + 3 is 1 << 5, not (1 << 2) + 3.
Wrong parse
1 << 2 + 3
32
Parenthesize
(1 << 2) + 3
7
When to use
Use it
- Defining bit flags
- Exact powers of two
- Binary protocol / bit-packing work
Reach for something else
- General multiplication → *
- Readable powers in non-hot code → 2 ** n
Notes
Complexity
O(bits of result)
Return
int — arbitrary precision
CPython impl
Objects/longobject.c :: long_lshift → __lshift__
Memory
Grows with the result size
Thread-safe
Yes — pure computation
FAQ
Yes, marginally — shifting is a single operation while ** goes through the general power path. It matters only in hot loops; prefer whichever reads better.
History
1.0
Core operator from the beginning.