Bitwise operators in Python
Bitwise operators in Python
We use bitwise operators in Python to work directly with bits, the smallest units of data in computers. These operators help us perform fast and efficient tasks like checking, setting, or changing specific bits in numbers. Understanding these can make your programs run quicker and use less memory.
Python bitwise operators are simple yet powerful tools. They are widely used in areas like game development, networking, and embedded systems to handle low-level data operations easily and effectively.
Types of Python Bitwise Operators
1. Bitwise Logical Operators
AND Operator
The AND operator returns 1 for each bit position where both operands have 1. Otherwise, it returns 0. This operator is often used to clear specific bits in an integer. Here’s an example of using the AND operator in Python:
Example
a = 60 # 60 = 0011 1100
b = 13 # 13 = 0000 1101
c = a & b # 12 = 0000 1100
print("Result of AND operation:", c)
Output
Result of AND operation: 12
OR Operator
The OR operator returns 1 for each bit position where at least one operand has 1. It returns 0 only if both operands have 0. This operator is commonly used to set specific bits in an integer. Here’s an example of using the OR operator in Python:
Example
a = 60 # 60 = 0011 1100
b = 13 # 13 = 0000 1101
c = a | b # 61 = 0011 1101
print("Result of OR operation:", c)
Output
Result of OR operation: 61
XOR Operator
The XOR operator returns 1 for each bit position where the operands have different bits. It returns 0 if both bits are the same. This operator is useful for flipping the bits of an integer. Here’s an example of using the XOR operator in Python:
Example
a = 60 # 60 = 0011 1100
b = 13 # 13 = 0000 1101
c = a ^ b # 49 = 0011 0001
print("Result of XOR operation:", c)
Output
Result of XOR operation: 49
NOT Operator
The NOT operator returns the complement of the operand, i.e., it changes each 1 to 0 and each 0 to 1. This operator is often used to reverse the bits of an integer. Here’s an example of using the NOT operator in Python:
Example
a = 60 # 60 = 0011 1100
c = ~a # -61 = 1100 0011
print("Result of NOT operation:", c)
Output
Result of NOT operation: -61
2. Bitwise Shift Operators
Left Shift Operator
The left shift operator moves the bits of the operand to the left by a specified number of positions. This effectively multiplies the operand by 2, raised to the power of the shift count. Here’s an example of using the left shift operator in Python:
Example
a = 60 # 60 = 0011 1100
b = a << 2 # 240 = 1111 0000
print("Result of left shift operation:", b)
Output
Result of left shift operation: 240
Right Shift Operator
The right shift operator moves the operand bits to the right by a specified number of positions. This effectively divides the operand by 2, raised to the power of the shift count. Here’s an example of using the right shift operator in Python:
Example
a = 60 # 60 = 0011 1100
b = a >> 2 # 15 = 0000 1111
print("Result of right shift operation:", b)
Output
Result of right shift operation: 15
 
 
 
 
 
 
 

No comments: