Table of contents
1.
Introduction
2.
Python Bitwise Operators: Overview & Examples
3.
Bitwise OR Operator 
3.1.
Example
3.2.
Python
4.
Bitwise XOR Operator
4.1.
Example
4.2.
Python
5.
Bitwise NOT Operator
5.1.
Example
6.
Python Bitwise Right Shift 
6.1.
Example
6.2.
Python
7.
Python Bitwise Left Shift
7.1.
Example
7.2.
Python
8.
Bitwise AND Operator
8.1.
Example
8.2.
Python
9.
Frequently Asked Questions
9.1.
What is the use of bitwise operators in real-world applications?
9.2.
Can bitwise operators be used with data types other than integers?
9.3.
How do bitwise shifts differ from arithmetic shifts?
10.
Conclusion
Last Updated: Jun 8, 2024
Easy

Python Bitwise AND Operators

Author Rinki Deka
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Bitwise operators in Python are special operators used to perform operations on binary numbers. They work on the individual bits of a number, allowing you to manipulate & compare them at a low level. Bitwise operators are commonly used in tasks such as encoding, decryption, & low-level system programming. 

Python Bitwise AND Operators

In this article, we'll talk about the different bitwise operators available in Python, their syntax, & how to use them with the help of examples. 

Python Bitwise Operators: Overview & Examples

Bitwise operations are essential in low-level programming where efficiency & direct memory manipulation are crucial. Python offers several bitwise operators that allow manipulation of individual bits of data, which can be incredibly useful for tasks such as image processing, cryptography, and network protocol development.

Below is a table summarizing the main bitwise operators in Python, with their syntax and function which will help you to understand everything properly : 

Python Bitwise Operators

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

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

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

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

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

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 DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. Also, check out some of the Guided Paths on topics such as Data Structure andAlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry.

Live masterclass