Bitwise Operators in Python: Types and Examples

Bitwise Operators in Python: Types and Examples

13 Sep 2024
Beginner
1.78K 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

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.

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.

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
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