Python Bitwise Operators
Introduction to Python Bitwise Operators
In Python, operators are special symbols or keywords that are used to perform operations on variables or values. Operators are a fundamental part of the language and are categorized into several types, each serving a specific purpose.
Bitwise operators in Python are employed to execute binary-level computations on integer values. These operators take the integer values, convert them into binary representations, and then carry out operations on individual bits or matching pairs of bits. This is why they are termed bitwise operators, as they operate at the binary level.
Bitwise Operators in Python
Python Bitwise operators are a set of operators in programming that allow you to perform bit-level operations on binary representations of integers. These operators manipulate individual bits or pairs of bits in binary numbers, enabling you to perform various low-level data manipulation and optimization tasks.
Here are the different types of bitwise operators:
- Bitwise AND(“&”)
- Bitwise OR(“|”)
- Bitwise XOR(“^”)
- Bitwise NOT(“~”)
- Bitwise Left Shift(“<<“)
- Bitwise Right Shift(“>>”)
Bitwise AND Operator in Python(&)
The bitwise AND operator, denoted as “&”, compares each pair of bits from two integers and returns a new integer where each bit is set to 1 if and only if both corresponding bits in the input integers are 1. In other words, if both bits are 1, the result is 1; otherwise, it’s 0.
a = 5 # 101 in binary b = 3 # 011 in binary result = a & b # 001 in binary print(result) # Output: 1
Bitwise OR Operator in Python(|)
The bitwise OR operator, denoted as “|”, compares each pair of bits from two integers and returns a new integer where each bit is set to 1 if at least one of the corresponding bits in the input integers is 1.
a = 5 # 101 in binary b = 3 # 011 in binary result = a | b # 111 in binary print(result) # Output: 7
Bitwise XOR Operator in Python(^)
The bitwise XOR (exclusive OR) operator, denoted as “^”, compares each pair of bits from two integers and returns a new integer where each bit is set to 1 if the corresponding bits in the input integers are different (one is 0 and the other is 1).
a = 5 # 101 in binary b = 3 # 011 in binary result = a ^ b # 110 in binary print(result) # Output: 6
Bitwise NOT Operator in Python(~)
The bitwise NOT operator, denoted as “~”, inverts all the bits in a given integer. It transforms 0s into 1s and 1s into 0s. It’s important to note that the result depends on the number of bits used to represent the integer.
a = 5 # 101 in binary result = ~a # -6 (varies depending on the number of bits) print(result)
Bitwise Left Shift (<>)
The bitwise left shift operator(<<) shifts the bits of an integer to the left by a specified number of positions, effectively multiplying the integer by 2 raised to the power of the shift count. The right shift operator (>>) shifts the bits to the right, effectively dividing the integer by 2 raised to the power of the shift count.
a = 5 # 101 in binary result = a << 2 # 10100 in binary, equivalent to 20 in decimal print(result) # Output: 20 result = a >> 1 # 10 in binary, equivalent to 2 in decimal print(result) # Output: 2
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
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
Applications of Python Bitwise Operators
To wrap it up:
Python Bitwise operators provide a powerful way to manipulate individual bits in integers, which can be useful for various low-level operations. Understanding how these operators work and when to use them is essential for tasks involving data manipulation, encoding, and algorithm optimization. Keep in mind that the behavior of the bitwise NOT operator may vary depending on the number of bits used to represent an integer, so it’s important to be aware of your system’s architecture when working with this operator.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Question 1.
Do bitwise operators have any impact on code performance?
Bitwise operators can be used for optimization in certain situations, but it’s essential to profile your code to confirm that they genuinely improve performance. Modern Python interpreters and compilers often optimize code well.
Question 2.
In what scenarios are bitwise operators most commonly used?
Bitwise operators are often used in low-level programming tasks, such as device drivers, data compression, and algorithmic optimizations. They are valuable for working with binary data and low-level hardware interactions.
Question 3.
What is the purpose of the bitwise XOR operator in Python?
The bitwise XOR operator returns a new integer where each bit is set to 1 if the corresponding bits in the input integers are different (one is 0 and the other is 1).
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
Login/Signup to comment