Bitwise Operators in Python

Bitwise Operators in Python

04 May 2024
Beginner
442 Views
8 min read

Bitwise Operator in Python: An Overview

We already discussed the Types of Operators in previous tutorial. In this Python tutorial, we'll explore the syntax, providing a comprehensive overview along with practical examples of bitwise operator.

To further enhance your understanding and application of bitwise operator's concepts, consider enrolling in the best Python Certification Course, to gain knowledge about effective utilization of unary operators for improved problem-solving and time management.

What is a Bitwise Operator in Python?

Bitwise operators in Python are used to perform bit-level operations on integers. These operators work on the binary representation of data, manipulating individual bits. Understanding bitwise operators is crucial when dealing with low-level programming or optimizing certain algorithms.

Syntax:

The syntax for bitwise operators in Python is concise and easy to understand. The primary bitwise operators include:

  • & (Bitwise AND)
  • | (Bitwise OR)
  • ~ (Bitwise NOT)
  • ^ (Bitwise XOR)
  • << (Bitwise Left Shift)
  • >> (Bitwise Right Shift)

Types of Bitwise Operators

1. Bitwise AND

The bitwise AND operator compares each bit of the first operand with the corresponding bit of the second operand. If both bits are 1, the resulting bit is set to 1; otherwise, it is set to 0.

result_and = 5 & 3  # Binary: 0101 & 0011 = 0001
print("Bitwise AND: 5 & 3 =", result_and)   

Output

Bitwise AND: 5 & 3 = 1

Read More - 50 Python Interview Questions and Answers

2. Bitwise OR

The bitwise OR operator compares each bit of the first operand to the corresponding bit of the second operand. If at least one bit is 1, the result is 1; otherwise, it is 0.

result_or = 5 | 3   # Binary: 0101 | 0011 = 0111
print("Bitwise OR: 5 | 3 =", result_or) 

Output

Bitwise OR: 5 | 3 = 7

3. Bitwise NOT

The bitwise NOT operator inverts each bit of its operand. It changes 1s to 0s and vice versa, effectively flipping the bits.

result_not = ~5     # Binary: ~0101 = 1010 (Two's complement)
print("Bitwise NOT: ~5 =", result_not)

Output

Bitwise NOT: ~5 = -6

4. Bitwise XOR

The bitwise XOR operator compares each bit of the first operand to the corresponding bit of the second operand. If the bits are different, the result is 1; if they are the same, the result is 0.

result_xor = 5 ^ 3  # Binary: 0101 ^ 0011 = 0110
print("Bitwise XOR: 5 ^ 3 =", result_xor)

Output

Bitwise XOR: 5 ^ 3 = 6

Read More - Python Developer Salary in India

5. Bitwise Shift Operators

  • Bitwise Left Shift

The bitwise left shift operator shifts the bits of the first operand to the left by a specified number of positions. Zeros are added to the vacant positions on the right.

result_left_shift = 5 << 1  # Binary: 0101 << 1 = 1010
print("Bitwise Left Shift: 5 << 1 =", result_left_shift)

Output

Bitwise Left Shift: 5 << 1 = 10
  • Bitwise Right Shift

The bitwise right shift operator shifts the bits of the first operand to the right by a specified number of positions. The vacant positions on the left are filled based on the sign bit for signed integers and with zeros for unsigned integers.

result_right_shift = 5 >> 1  # Binary: 0101 >> 1 = 0010
print("Bitwise Right Shift: 5 >> 1 =", result_right_shift)

Output

Bitwise Right Shift: 5 >> 1 = 2

Examples of Bitwise Operators in Python Compiler

# Bitwise AND
result_and = 5 & 3  # Result: 1

# Bitwise OR
result_or = 5 | 3   # Result: 7

# Bitwise NOT
result_not = ~5     # Result: -6

# Bitwise XOR
result_xor = 5 ^ 3  # Result: 6

# Bitwise Left Shift
result_left_shift = 5 << 1  # Result: 10

# Bitwise Right Shift
result_right_shift = 5 >> 1  # Result: 2

# Displaying Results
print("Bitwise AND: 5 & 3 =", result_and)
print("Bitwise OR: 5 | 3 =", result_or)
print("Bitwise NOT: ~5 =", result_not)
print("Bitwise XOR: 5 ^ 3 =", result_xor)
print("Bitwise Left Shift: 5 << 1 =", result_left_shift)
print("Bitwise Right Shift: 5 >> 1 =", result_right_shift)

Output

Bitwise AND: 5 & 3 = 1
Bitwise OR: 5 | 3 = 7
Bitwise NOT: ~5 = -6
Bitwise XOR: 5 ^ 3 = 6
Bitwise Left Shift: 5 << 1 = 10
Bitwise Right Shift: 5 >> 1 = 2
Summary
Bitwise operators in Python provide a powerful way to manipulate individual bits within integers. They are particularly useful in scenarios involving low-level programming, optimization, and certain mathematical operations. Understanding how to apply these operators can lead to more efficient code and improved performance.
Additionally, if you're looking to validate your understanding of Python operators and enhance your programming skills, consider pursuing a Python Certification

FAQs

Q1. When should I use bitwise operators in Python?

Bitwise operators are often used in scenarios involving low-level programming, cryptography, and optimization of certain algorithms. They can be useful for tasks such as setting or clearing specific bits in a binary representation.

Q2. Are bitwise operators exclusive to integers in Python?

Yes, bitwise operators primarily operate on integers in Python. While you can apply them to other numeric types, the results may not be as expected, and it's important to consider the binary representation of the data.

Q3. How do bitwise operators contribute to performance optimization?

Bitwise operators can be more efficient than their logical counterparts for certain operations, as they directly manipulate the binary representation of data. This can lead to faster execution times in scenarios where bit-level manipulation is required.
Share Article

Live Classes Schedule

Our learn-by-building-project method enables you to build practical/coding experience that sticks. 95% of our learners say they have confidence and remember more when they learn by building real world projects.
Angular Certification Course Jun 16 SAT, SUN
Filling Fast
09:30AM to 11:30AM (IST)
Get Details
Advanced Full-Stack .NET Developer Certification Training Jun 17 MON, WED, FRI
Filling Fast
07:00AM to 08:30AM (IST)
Get Details
ASP.NET Core Certification Training Jun 17 MON, WED, FRI
Filling Fast
07:00AM to 08:30AM (IST)
Get Details
.NET Microservices Certification Training Jun 23 SAT, SUN
Filling Fast
05:30PM to 07:30PM (IST)
Get Details
ASP.NET Core (Project) Jun 23 SAT, SUN
Filling Fast
08:30PM to 10:30PM (IST)
Get Details
Azure Developer Certification Training Jun 23 SAT, SUN
Filling Fast
07:00AM to 09:00AM (IST)
Get Details
React JS Certification Training | Best React Training Course Jun 30 SAT, SUN
Filling Fast
08:30PM to 10:30PM (IST)
Get Details
ASP.NET Core Certification Training Jun 30 SAT, SUN
Filling Fast
10:00AM to 12:00PM (IST)
Get Details
Generative AI For Software Developers Jul 14 SAT, SUN
Filling Fast
08:30PM to 10:30PM (IST)
Get Details

Can't find convenient schedule? Let us know

About Author
Sakshi Dhameja (Author and Mentor)

She is passionate about different technologies like JavaScript, React, HTML, CSS, Node.js etc. and likes to share knowledge with the developer community. She holds strong learning skills in keeping herself updated with the changing technologies in her area as well as other technologies like Core Java, Python and Cloud.

Accept cookies & close this