Shift Operator in Python
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
Program for Left Shift Operator in Python
Here’s a code snippet :
# 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 # Display the original number, shift count, and the result without f-strings 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 :
This Python program showcases the use of the left shift operator (<<) to shift the binary representation of an input integer to the left by a specified number of positions. Users input an integer and the desired shift count. The program then presents the original number, shift count, and the result without employing formatted strings. This demonstrates how the left shift operator efficiently multiplies an integer by 2 raised to the power of the shift count. In the given example, the original number, 10, is left-shifted by 2 positions, yielding 40.
Program for Right Shift Operator in Python
Here’s a code snippet :
# 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 # Display the original number, shift count, and the result without f-strings print("Original number:", number) print("Right shift by", shift_count, "positions:", result)
Here’s a code snippet :
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 Python program utilizes the right shift operator (>>) to shift the binary representation of an entered integer to the right by a specified number of positions. The user inputs an integer and the shift count. The program then displays the original number, shift count, and the result without using formatted strings. This demonstrates how right shift operator can be employed to effectively divide an integer by 2 raised to the power of the shift count. In the provided example, the original number 20 is right-shifted by 2 positions, resulting in 5.
Important Points for Shift Operator in Python
- 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.
- 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.
- 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:
In conclusion, 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.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Question 1.
What are some practical applications of shift operators?
Shift operators are used in data compression, cryptography, low-level programming, optimization, and bitmasking, among other applications. They are also helpful for efficient multiplication and division by powers of 2.
Question 2.
Can I shift bits beyond the size of the integer data type?
Shifting bits beyond the size of the integer data type may lead to unexpected results or errors, so it’s essential to be cautious and aware of the integer’s representation size.
Question 3.
Are there any caveats or potential pitfalls when using shift operators?
It’s important to be aware of the data size, potential loss of information, and the specific requirements of your application when using shift operators. Understanding binary representation is crucial to avoid unintended results.
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