Shift Operator in Python

Shift operator

Introduction

Python, a versatile and powerful programming language, provides a range of operators for performing various operations on data. Among these operators shift operator in python consist of left shift (<<) and right shift (>>) operators, which are used for bitwise manipulation of integers. These operators allow you to shift the binary representation of a number’s bits to the left or right, effectively multiplying or dividing the number by powers of 2. 

Shift Operator in Python

A shift operator is a type of bitwise operator used in computer programming to perform bitwise shift operations on the binary representation of integers. It allows you to shift the individual bits of a binary number to the left or right by a specified number of positions.

There are two primary shift operators:

  • Left Shift Operator (<<)
  • Right Shift Operator (>>)

Left Shift Operator (<<)

The left shift operator, denoted as “<<“, shifts the bits of a binary number to the left by a specified number of positions. This operation is equivalent to multiplying the number by 2 raised to the power of the shift count. It effectively adds zeros to the right of the binary representation, moving the bits to higher positions.

Syntax :

result = number << shift_count

Example :

# Using the “left shift operator”
number = 10
shift_count = 2
result = number << shift_count # This is equivalent to 10 * (2**2)
print(result) # Output: 40

Right Shift Operator (>>)

The right shift operator (>>) is used to shift the bits of a binary number to the right by a specified number of positions. Shifting bits to the right effectively divides the number by 2 raised to the power of the shift count.

Syntax :

result = number >> shift_count

Example :

# Using the “right shift operator”

number = 20 shift_count = 2

result = number >> shift_count # This is equivalent to 20 // (2**2)

print(result) # Output: 5

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Program for Shift Operator in Python​

Shift operators in Python are used to shift the bits of a number to the left or right. The left shift operator (<<) moves bits to the left, adding zeros on the right, effectively multiplying the number by 2𝑛. The right shift operator (>>) moves bits to the right, discarding bits on the right, effectively dividing the number by 2𝑛. These operators are commonly used in low level programming, bit manipulation, and optimization tasks.

Program for Left Shift Operator in Python

The left shift operator (<<) in Python shifts the bits of a number to the left by a specified number of positions, effectively multiplying it by 2 for each shift. It’s commonly used in low level programming and performance optimization scenarios.

Run
# Program: Left Shift Operator with User Input

# Input an integer
number = int(input("Enter an integer: "))

# Input the number of positions to shift
shift_count = int(input("Enter the number of positions to shift to the left: "))

# Perform the left shift operation
result = number << shift_count  # Equivalent to number * (2 ** shift_count)

# Display the results
print("Original number:", number)
print("Left shift by", shift_count, "positions:", result)

Output :

Enter an integer: 10
Enter the number of positions to shift to the left: 2
Original number: 10
Left shift by 2 positions: 40

Explanation :

  • number << shift_count shifts the bits of number (10) to the left by 2 positions.
  • Each left shift is equivalent to multiplying the number by 2, so 10 << 2 = 10 * (2^2) = 40.
  • In binary: 10 is 1010. Shifting left by 2 bits → 101000, which is 40 in decimal.

Time and Space Complexity:

Type of ComplexityValue
Time ComplexityO(1)
Space ComplexityO(1)

Program for Right Shift Operator in Python

The right shift operator (>>) in Python shifts the bits of a number to the right by a specified number of positions, effectively dividing the number by 2 for each shift. It is commonly used for bitwise operations and efficient arithmetic in low-level programming.

Run
# Program: Right Shift Operator with User Input

# Input an integer
number = int(input("Enter an integer: "))

# Input the number of positions to shift
shift_count = int(input("Enter the number of positions to shift to the right: "))

# Perform the right shift operation
result = number >> shift_count  # Equivalent to number // (2 ** shift_count)

# Display the original number, shift count, and the result without f-strings
print("Original number:", number)
print("Right shift by", shift_count, "positions:", result)

Output :

Enter an integer: 20
Enter the number of positions to shift to the right: 2
Original number: 20
Right shift by 2 positions: 5

Explanation :

  • The >> operator shifts the bits of a number to the right, effectively performing integer division by powers of 2.
  • number >> shift_count is equivalent to number // (2 ** shift_count).
  • In this case, 20 >> 2 is equivalent to 20 // 4, which results in 5.
  • Binary of 20 is 10100. After shifting right by 2 bits, we get 101, which is 5 in decimal.
  • Right shift is commonly used in low-level programming, optimizations, and embedded systems.

Time and Space Complexity:

Type of ComplexityValue
Time ComplexityO(1)
Space ComplexityO(1)

Important Points for Shift Operator in Python

  1. Using the left-shift and right-shift operators with negative numbers is discouraged, as it leads to undefined behavior. For instance, the results of operations like “1 >> -1” and “1 << -1” are undefined.
  2. When shifting a number by more positions than the integer’s size allows, the behavior becomes undefined. For instance, attempting “1 << 33” is undefined in a 32-bit integer context. For larger bit shifts, consider using an ULL (Unsigned Long Long) to accommodate the larger values in a 64-bit context, as seen in 1ULL << 62.
  3. Left-shifting a number by 1 or right-shifting by 1 is akin to multiplying the initial value by 2 raised to a specified power (e.g., 1 << 3 equals 1 * 2^3) and dividing the initial value by 2 raised to the same power (e.g., 1 >> 3 equals 1 / 2^3) respectively.

Applications of Bitwise Shift Operators

To wrap it up:

The shift operators, both the left shift (<<) and right shift (>>), are powerful tools in the Python programmer’s toolbox. These operators enable efficient bitwise manipulation, enabling tasks such as multiplying or dividing integers by powers of 2, bit manipulation, data compression, cryptography, and low-level programming. The constant time and space complexities of shift operations make them invaluable for optimizing code, particularly when dealing with large datasets.

FAQs

Shift operators in Python are used to shift the bits of a number left or right. The two types are << (left shift) and >> (right shift).

The left shift operator moves bits to the left and fills 0s on the right, effectively multiplying the number by 2 for each shift.

The right shift operator shifts bits to the right and discards the least significant bits, dividing the number by 2 for each shift.

Yes, but right shifting negative numbers keeps the sign bit (arithmetic shift), which may lead to different results than with positive numbers.

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription