Python 3 – Bitwise Operators Example
Python is a popular programming language that is easy to learn and use. It has several operators that you can use to perform operations on data. One such operator is the bitwise operator. In this article, we will discuss what bitwise operators are and how you can use them in Python 3.
What Are Bitwise Operators?
Bitwise operators are used to perform operations on bits. A bit is a binary digit that can have only two values, 0 and 1. Bitwise operators work at the bit level, hence the name “bitwise”. There are six bitwise operators in Python:
&
– AND Operator|
– OR Operator^
– XOR Operator~
– Negation Operator<<
– Left Shift Operator>>
– Right Shift Operator
Let’s look at each of these operators in detail.
AND Operator
The AND operator returns 1 if both bits are 1, otherwise it returns 0. Here’s an example:
a = 5 # 101 in binary
b = 3 # 011 in binary
result = a & b
print(result) # Output: 1
# 101 & 011 = 001
In the above example, a & b
returns 1 because the third bit in a
and b
is 1.
OR Operator
The OR operator returns 1 if at least one of the bits is 1, otherwise it returns 0. Here’s an example:
a = 5 # 101 in binary
b = 3 # 011 in binary
result = a | b
print(result) # Output: 7
# 101 | 011 = 111
In the above example, a | b
returns 7 because the first, second, and third bits in a
and b
are 1.
XOR Operator
The XOR operator returns 1 if one of the bits is 1 and the other is 0, otherwise it returns 0. Here’s an example:
a = 5 # 101 in binary
b = 3 # 011 in binary
result = a ^ b
print(result) # Output: 6
# 101 ^ 011 = 110
In the above example, a ^ b
returns 6 because the first and third bits in a
and b
are different.
Negation Operator
The negation operator returns the complement of a number. Here’s an example:
a = 5 # 101 in binary
result = ~a
print(result) # Output: -6
# ~101 = -110
In the above example, ~a
returns -6 because 101
is equal to 00000101
in 8-bit binary representation, and the negation of this number is 11111010
in 8-bit binary representation. This is equivalent to -6 in decimal representation.
Left Shift Operator
The left shift operator shifts the bits of a number to the left by a certain number of positions. Here’s an example:
a = 5 # 101 in binary
result = a << 2
print(result) # Output: 20
# 101 shifted left by 2 positions becomes 10100, which is equal to 20 in decimal
In the above example, a << 2
shifts the bits of a
two positions to the left, resulting in the number 10100
, which is equivalent to 20 in decimal representation.
Right Shift Operator
The right shift operator shifts the bits of a number to the right by a certain number of positions. Here’s an example:
a = 5 # 101 in binary
result = a >> 1
print(result) # Output: 2
# 101 shifted right by 1 position becomes 10, which is equal to 2 in decimal
In the above example, a >> 1
shifts the bits of a
one position to the right, resulting in the number 10
, which is equivalent to 2 in decimal representation.
Conclusion
Bitwise operators are very useful when working with binary data. They allow you to perform operations at the bit level, which is often necessary when working with low-level data such as hardware registers, network protocols, and encryption algorithms. In this article, we discussed the six bitwise operators in Python 3 and how to use them with examples.