Table of Contents
Python Bitwise operators are mostly used with integer-type objects. Hence, Bitwise operation will be executed through bitwise calculations on integers. Read this article to learn more about the Python Bitwise operator.
What is Python Bitwise Operator
Python Bitwise operators will work with integers. Whereas, they will consider the object as a string instead of treating it as a whole. Thus, different operations will be done on each bit in a string.
Moreover, Python has six bitwise operators such as &, |, ^, ~, << and >>. Each of these operators is mostly binary. This means they can work on the two operands. Each operand is referred to as a binary digit(bit) ie, 1 and 0.
Let us take a look at the table of the different operators of Bitwise
What is Bitwise And Operator?
The Bitwise AND operator works similarly to the logical AND operator. This will provide the result as true, if both the operands will come as 1. The combinations are given below
0 & 0 is 0
1 & 0 is 0
0 & 1 is 0
1 & 1 is 1
OPERATOR | NAME | DESCRIPTION | SYNTAX |
---|---|---|---|
& | Bitwise AND | Bit wise operator has result bit 1, if both operand bits are 1; or results bit 0. | x & y |
~ | Bitwise NOT | Bitwise NOT operator will invert the individual bits | ~x |
| | Bitwise Or | This operator will give the result bit as 1 only if the operand bit is 1 or it will give 0 | x | y |
^ | Bitwise XOR | This operator will provide results bit 1 only if any of the operand bit is 1 , or else it will result bit in 0. | x ^ y |
<< | Bitwise Left shift | The left operand value will be moved toward’s the right by checking the number of bits. | x<< |
>> | Bitwise Right shift | The left operand value will be moved toward’s the right by checking the number of bits. |
Example
a = 10 = 1010 (Binary)
b = 4 = 0100 (Binary)
a & b = 1010
&
0100
= 0000
= 0 (Decimal)
What is Bitwise OR Operator?
This symbol “|” is referred to as Pipe and is also known as Bitwise OR Operator. Only if the bit operand is 1, then it will result in 1, or else it is 0
0 | 0 is 0
0 | 1 is 1
1 | 0 is 1
1 | 1 is 1
Example
a = 10 = 1010 (Binary)
b = 4 = 0100 (Binary)
a | b = 1010
|
0100
= 1110
= 14 (Decimal)
What is Bitwise NOT operator?
This bitwise operator is referred to as the binary equivalent of logical NOT operators. It will flip each bit, so, 1 will be replaced by 0 and 0 by 1. Hence, it will return the complement of the original number.
Moreover, python will use 2’s complement method. For example, positive integers can be achieved by reversing the bits. In contrast, negative numbers, -x will be written with the bit pattern for (x-1) along all the bits complemented ( switched from 1 to 0 or 0 to 1).
-1 is complement(1 - 1) = complement(0) = "11111111"
-10 is complement(10 - 1) = complement(9) = complement("00001001") = "11110110".
Example
a = 10 = 1010 (Binary)
In computers we usually represent numbers using 32 bits,
so binary representation of 10 is (....0000 1010)[32 bits]
~a is basically 1's complement of a
i.e ~a should be ~10 = ~(....0000 1010) = (....1111 0101) = intermediate-result
Since bitwise negation inverts the sign bit,
we now have a negative number. And we represent a negative number
using 2's complement.
2's complement of intermediate-result is:
intermediate-res = 0101 //....1111 0101
1010 //....0000 1010 -(1's complement)
+1
-----------
= 1011 //....0000 1011
-----------
= -11 (Decimal)
thus ~a = -11
What is Bitwise XOR Operator?
The full form of XOR is the exclusive OR. So, the result that is provided by the OR operator on two bits is 1, or else one of the bits is 1.
0 ^ 0 is 0
0 ^ 1 is 1
1 ^ 0 is 1
1 ^ 1 is 0
Example
a = 10 = 1010 (Binary)
b = 4 = 0100 (Binary)
a ^ b = 1010
^
0100
= 1110
= 14 (Decimal)
Python Program on the bitwise operator will be given below
# Python program to show
# bitwise operators
a = 10
b = 4
# Print bitwise AND operation
print("a & b =", a & b)
# Print bitwise OR operation
print("a | b =", a | b)
# Print bitwise NOT operation
print("~a =", ~a)
# print bitwise XOR operation
print("a ^ b =", a ^ b)
Output
a & b = 0
a | b = 14
~a = -11
a ^ b = 14
What is a Shift Operator?
This Shift operator functions to shift the bits of a number left or right and then multiply or divide the number by two respectively. This operator is mainly used to multiply or divide a number by two.
Bitwise right shift
This operator is used to shift the bits of the number to the right and will fill 0 on the voids left as the result. Therefore, a similar effect can be achieved by dividing the number with some power of two.
Example
Example 1:
a = 10 = 0000 1010 (Binary)
a >> 1 = 0000 0101 = 5
Example 2:
a = -10 = 1111 0110 (Binary)
a >> 1 = 1111 1011 = -5
Bitwise left shift
This operator will be used to shift the bits to the number to the left and that will fill 0 on voids right as the end result. Whereas, a similar effect can be achieved through multiplying the number with some power of two.
Example
Example 1:
a = 5 = 0000 0101 (Binary)
a << 1 = 0000 1010 = 10
a << 2 = 0001 0100 = 20
Example 2:
b = -10 = 1111 0110 (Binary)
b << 1 = 1110 1100 = -20
b << 2 = 1101 1000 = -40
Python program is illustrated below to show the shift operators.
# Python program to show
# shift operators
a = 10
b = -10
# print bitwise right shift operator
print("a >> 1 =", a >> 1)
print("b >> 1 =", b >> 1)
a = 5
b = -10
# print bitwise left shift operator
print("a << 1 =", a << 1)
print("b << 1 =", b << 1)
Output
a >> 1 = 5
b >> 1 = -5
a << 1 = 10
b << 1 = -20
What is Bitwise Operator Overloading?
Bitwise Operator Overloading is referred to as the extended meaning that is beyond the predefined operational meaning. For instance, operator
+ mostly functions to add the two integers and even join the two strings and combine the two lists.
Moreover, this can be gained a the + will be overloaded with the int class and str class. Operator Overloading consists of the same built-in operator or can work to demonstrate the different behavior of objects in the different classes.
An example to show the Bitwise Operator Overloading is given below
# Python program to demonstrate
# operator overloading
class Geek():
def __init__(self, value):
self.value = value
def __and__(self, obj):
print("And operator overloaded")
if isinstance(obj, Geek):
return self.value & obj.value
else:
raise ValueError("Must be a object of class skillvertex")
def __or__(self, obj):
print("Or operator overloaded")
if isinstance(obj, Geek):
return self.value | obj.value
else:
raise ValueError("Must be a object of class skillvertex")
def __xor__(self, obj):
print("Xor operator overloaded")
if isinstance(obj, Geek):
return self.value ^ obj.value
else:
raise ValueError("Must be a object of class Skilvertex")
def __lshift__(self, obj):
print("lshift operator overloaded")
if isinstance(obj, Geek):
return self.value << obj.value
else:
raise ValueError("Must be a object of class skillvertex")
def __rshift__(self, obj):
print("rshift operator overloaded")
if isinstance(obj, Geek):
return self.value >> obj.value
else:
raise ValueError("Must be a object of class skill vertex")
def __invert__(self):
print("Invert operator overloaded")
return ~self.value
# Driver's code
if __name__ == "__main__":
a = skillvertex(10)
b = skillvertex(12)
print(a & b)
print(a | b)
print(a ^ b)
print(a << b)
print(a >> b)
print(~a)
Output
And operator overloaded
8
Or operator overloaded
14
Xor operator overloaded
8
lshift operator overloaded
40960
rshift operator overloaded
8
Invert operator overloaded
-11
Conclusion
To conclude, this article will improve your knowledge of the different types of Python Bitwise operators. Those are Bitwise AND operator, Bitwise OR operator, Bitwise NOT operator, and several others are included.
Python Bitwise Operator- FAQs
Q1. What is a bitwise operator in Python?
Ans. The bitwise operator will function to do the bitwise calculations on integers.
Q2. What is == and != in Python?
Ans. These are used to compare the value of the two objects.
Q3. What is a tuple in Python?
Ans. It is a data structure type that works similarly to the list.
Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together