Python
Operators
Bitwise
Programming
Python Syntax

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.

python
1value = 5          # binary: 101
2shifted = value << 2
3
4print(shifted)

This prints 20, because:

  • '5 in binary is 101'
  • shifting left by two gives 10100
  • '10100 in decimal is 20'

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.

python
1value = 20         # binary: 10100
2shifted = value >> 2
3
4print(shifted)

This prints 5, because:

  • '20 in binary is 10100'
  • shifting right by two gives 101
  • '101 in decimal is 5'

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:

python
1def bits(n):
2    return format(n, "08b")
3
4
5value = 13
6print(bits(value))
7print(bits(value << 1))
8print(bits(value >> 2))

Output:

text
00001101
00011010
00000011

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:

python
1READ = 1 << 0
2WRITE = 1 << 1
3EXECUTE = 1 << 2
4
5permissions = READ | WRITE
6
7print(permissions)
8print(bool(permissions & WRITE))
9print(bool(permissions & EXECUTE))

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.

python
print(-8 >> 1)
print(-3 >> 1)

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.

Course illustration
Course illustration

All Rights Reserved.