Bitwise OR Operator
The bitwise OR operator (|) compares each bit of its first operand to the corresponding bit of its second operand. If either bit is 1, the corresponding result bit is set to 1.
Example
Python
a = 10 # in binary: 1010
b = 4 # in binary: 0100
result = a | b # in binary: 1110
print("Bitwise OR of 10 and 4 is:", result)

You can also try this code with Online Python Compiler
Run Code
Output:
14
Bitwise XOR Operator
The bitwise XOR operator (^) compares each bit of its first operand to the corresponding bit of its second operand. If only one of these bits is 1, the corresponding result bit is set to 1.
Example
Python
a = 10 # in binary: 1010
b = 4 # in binary: 0100
result = a ^ b # in binary: 1110
print("Bitwise XOR of 10 and 4 is:", result)

You can also try this code with Online Python Compiler
Run Code
Output:
14
Bitwise NOT Operator
The bitwise NOT operator (~) inverts all the bits of its operand, which means that it makes every 0 a 1 & every 1 a 0.
Example
a = 10 # in binary: 1010
result = ~a # in binary after inversion: ...11110101
print("Bitwise NOT of 10 is:", result)
Output depends on the system and is typically -11 in a signed representation
Python Bitwise Right Shift
The right shift operator (>>) shifts the bits of the number to the right & fills the left end with the same as the leftmost bit.
Example
Python
a = 10 # in binary: 1010
result = a >> 2 # shifts right by 2, result in binary: 0010
print("Right shift by 2 on 10 is:", result)

You can also try this code with Online Python Compiler
Run Code
Output:
2
Python Bitwise Left Shift
The left shift operator (<<) shifts the bits of the number to the left & fills the right end with 0s.
Example
Python
a = 10 # in binary: 1010
result = a << 2 # shifts left by 2, result in binary: 101000
print("Left shift by 2 on 10 is:", result)

You can also try this code with Online Python Compiler
Run Code
Output:
40
Bitwise AND Operator
The bitwise AND operator (&) compares each bit of its first operand to the corresponding bit of its second operand. If both bits are 1, the corresponding result bit is set to 1; otherwise, it is set to 0.
Example
Python
a = 10 # in binary: 1010
b = 4 # in binary: 0100
result = a & b # in binary: 0000
print("Bitwise AND of 10 & 4 is:", result)

You can also try this code with Online Python Compiler
Run Code
Output:
0
Frequently Asked Questions
What is the use of bitwise operators in real-world applications?
Bitwise operators are crucial in tasks that require direct data manipulation, such as digital image processing, encryption algorithms, & network data packet management.
Can bitwise operators be used with data types other than integers?
In Python, bitwise operators are primarily designed for integer data types. Applying them to other data types requires conversion to integers first.
How do bitwise shifts differ from arithmetic shifts?
Bitwise shifts in Python (>> & <<) manipulate the binary representation directly, often filling shifted positions with the same bit as the edge or zero. Arithmetic shifts, while similar, are designed to handle signed numbers appropriately, preserving the sign bit.
Conclusion
In this article, we have learned about the bitwise operators available in Python. We explained the different operators, including bitwise AND, OR, XOR, NOT, left shift, & right shift. We discussed their syntax & behavior, with the help of examples. The proper knowledge of bitwise operators is essential for tasks involving binary manipulation, such as encoding, decryption, & low-level system programming.
You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSA, DBMS, Competitive Programming, Python, Java, JavaScript, etc. Also, check out some of the Guided Paths on topics such as Data Structure andAlgorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry.