>>

Shift bits right — halving with a floor, so -7 >> 1 is -4, matching //.

Bitwise operatorPython 1.0+Live demo
Common call
x >> 8
Returns
a // 2**n — floored, sign-preserving
Replaces
extracting bytes: (rgb >> 16) & 0xFF
Watch out
negatives floor down: -7 >> 1 == -4
aaThe value to shift.type: int · required >> nnHow many places — must be non-negative.type: int · required
int

Demo

Live evaluation
Try:
Inputs
aintvalue
nintplaces
Output
16 >> 2
4

Each place halves with a floor — note -7 >> 1 giving -4, exactly like -7 // 2. The byte-extract case shifts the red channel of 0xFF0000 down to 255.

Operands

NameTypeRequiredDescription
aintyesThe value to shift.
nintyesHow many places — must be non-negative.

Return value

inta with its bits moved n places right — exactly a // 2**n, flooring like // does. The sign is preserved (arithmetic shift).

Common patterns

Extracting bit fields
Shift down, then mask.
red   = (rgb >> 16) & 0xFF
green = (rgb >> 8)  & 0xFF
blue  = rgb         & 0xFF
Halving in binary algorithms
Binary search on ints, fast average without overflow risk elsewhere.
mid = (lo + hi) >> 1   # same as // 2

Examples

1. Basic shift
16 >> 2
Returns
4
2. Floors negatives
-7 >> 1
Returns
-4
3. Byte extraction
(0xFF0000 >> 16) & 0xFF
Returns
255

Pitfalls

1. No unsigned (logical) shift
Python has no >>> — negative numbers stay negative under >>.
Stays negative
-8 >> 1
-4, never a huge positive
N-bit unsigned view
(-8 & 0xFFFFFFFF) >> 1
mask first for 32-bit behavior
2. Shifting everything away
Shift counts past the bit length give 0 (or -1 for negatives) — silently.
All gone
5 >> 10
0
Sanity-check counts
assert n < a.bit_length()
catch over-shifts

When to use

Use it
  • Unpacking bit fields and bytes
  • Floor-halving in bit-level algorithms
Reach for something else
  • General division → // or /
  • Unsigned semantics → mask with & first

Notes

Complexity
O(bits)
Return
int
CPython impl
Objects/longobject.c :: long_rshift → __rshift__
Memory
No allocation for small ints
Thread-safe
Yes — pure computation

FAQ

Unsigned shift only makes sense with fixed-width integers. Python ints are unbounded, so there is no sign bit position to shift zeros into — mask to a width first if you need that behavior.

History

1.0
Core operator from the beginning.