Python > Core Python Basics > Basic Operators > Bitwise Operators (&, |, ^, ~, <<, >>)
Understanding Bitwise Operators in Python
This snippet demonstrates the use of bitwise operators in Python. Bitwise operators work on individual bits within integers. Understanding these operators is crucial for low-level programming, data manipulation, and optimization.
Basic Bitwise Operators: AND, OR, XOR, NOT
This code demonstrates the core bitwise operators:
# Bitwise AND (&)
a = 5 # 0101 in binary
b = 3 # 0011 in binary
result_and = a & b # 0001 in binary, which is 1
print(f"a & b = {result_and}")
# Bitwise OR (|)
result_or = a | b # 0111 in binary, which is 7
print(f"a | b = {result_or}")
# Bitwise XOR (^)
result_xor = a ^ b # 0110 in binary, which is 6
print(f"a ^ b = {result_xor}")
# Bitwise NOT (~)
result_not_a = ~a # Inverts all bits. Due to two's complement representation, it's -6
print(f"~a = {result_not_a}")
#Bitwise left shift (<<)
result_left = a << 1 # Shifts bits left by 1 position. 0101 becomes 1010, which is 10
print(f"a << 1 = {result_left}")
#Bitwise right shift (>>)
result_right = a >> 1 # Shifts bits right by 1 position. 0101 becomes 0010, which is 2
print(f"a >> 1 = {result_right}")
Concepts Behind the Snippet
Bitwise operators manipulate data at the bit level, which is fundamental to computer science. Understanding how these operators function is crucial for:
Real-Life Use Case Section
Consider a scenario where you need to represent a set of boolean flags (e.g., permissions for a file). Instead of using separate boolean variables, you can use a single integer and bitwise operators to represent these flags. For example:
To check if a file has Read permission, you would use the bitwise AND operator: permissions & READ_FLAG
. If the result is non-zero, the file has read permissions.
Best Practices
Interview Tip
Be prepared to explain the purpose of each bitwise operator and provide examples of their use. Common interview questions include:
When to Use Them
Bitwise operators are particularly useful in the following scenarios:
Memory Footprint
Bitwise operations generally operate on integers, which occupy a fixed amount of memory depending on the system architecture (e.g., 32-bit or 64-bit). Since bitwise operators work directly on the bits of these integers, their memory footprint is typically very small. In scenarios requiring storage and manipulation of boolean flags or sets, leveraging an integer and bitwise operations can provide memory efficiency, surpassing the use of individual boolean variables.
Alternatives
Alternatives to bitwise operators depend on the specific use case. For example:
and
, or
, and not
instead of &, |, and ~.set
data structure might be more readable and maintainable, although potentially less memory-efficient for a small number of flags.
Pros
Cons
FAQ
-
What is the difference between & and && in Python?
& is the bitwise AND operator, which operates on the individual bits of integers. && is a logical AND operator (available in some other languages like C++ and Java) but not used in Python. In Python you use `and` for logical AND operations on boolean values. -
How can I check if a number is even or odd using bitwise operators?
You can use the bitwise AND operator with 1. If the result is 0, the number is even; otherwise, it's odd. For example:if (number & 1) == 0: # Even
-
How does the bitwise NOT operator work on negative numbers?
In Python, integers are represented using two's complement. The bitwise NOT operator inverts all bits, including the sign bit. Therefore, the result of~x
is-(x+1)
.