What do and mean in Python?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
This question is about Python's bitwise shift operators >> and <<. They move the bits of an integer right or left by a given number of positions. These operators are useful in low-level numeric code, bit masks, compact flag handling, and some performance-sensitive arithmetic, but they are only clear if you understand how the underlying binary representation changes.
Left Shift with <<
The left-shift operator moves bits to the left. For non-negative integers, shifting left by n positions is equivalent to multiplying by 2 ** n.
This prints 20, because:
- '
5in binary is101' - shifting left by two gives
10100 - '
10100in decimal is20'
This arithmetic interpretation is often the easiest way to reason about left shift for positive integers.
Right Shift with >>
The right-shift operator moves bits to the right. For non-negative integers, shifting right by n positions is roughly the same as integer division by 2 ** n.
This prints 5, because:
- '
20in binary is10100' - shifting right by two gives
101 - '
101in decimal is5'
For positive values, that mental model works well. For negative integers, the details are more subtle because Python keeps integer sign semantics instead of exposing a fixed-width machine integer directly.
See the Bit Patterns Explicitly
A small helper makes shifts easier to visualize:
Output:
This is often the fastest way to build intuition for how shifts interact with masks and flags.
Common Use Cases
Bit shifting appears in a few common patterns:
- multiply or divide powers of two
- pack or unpack flags into one integer
- manipulate binary protocols or file formats
- isolate bit fields
Example of building a mask:
This is a normal and readable use of shifts because the code is clearly working with flags.
Be Careful with Negative Numbers
Python integers are not fixed-width in the same way C integers are. Right-shifting negative numbers keeps the sign behavior you would expect from arithmetic shift semantics.
This is one reason bitwise code should be written carefully and tested with representative values. The operators are simple, but assumptions imported from other languages can still lead to bugs.
For most application code, if you are using shifts only as a clever substitute for normal multiplication or division, plain arithmetic may be more readable.
Common Pitfalls
- Using bit shifts without understanding whether the code is meant to operate on signed or non-negative integers.
- Treating shifts as a readability improvement when ordinary multiplication or division would be clearer.
- Forgetting that Python integers are not fixed-width machine integers by default.
- Writing bitwise code without checking the actual binary representation during debugging.
- Confusing shift operators with logical operators or comparison syntax.
Summary
- '
<<shifts bits left and usually acts like multiplication by powers of two for non-negative integers.' - '
>>shifts bits right and usually acts like integer division by powers of two for non-negative integers.' - These operators are most useful for masks, flags, and binary data handling.
- Binary visualization helps prevent mistakes when learning or debugging bitwise code.
- Use shifts when they express real bit manipulation, not just because they look clever.

