Operator overloading means giving extended meaning to an operator beyond its predefined meaning. It provides an additional ability for any operator to react depending upon the scenario.
For instance, the +
operator can be used to add two integer values to concatenate two or more strings. Hence, it is called operator overloading because, in a standard string library, the +
operator is overloaded for concatenation.
Let's take a look at bitwise operator overloading in detail.
AND
operatorLet's look at an example of the bitwise AND
operator's overloading.
# Python program to demonstrate# operator overloadingclass Test():def __init__(self, value):self.value = value# magic function to perform logical ANDdef __and__(self, obj):print("Overloaded AND Operator")if isinstance(obj, Test):return self.value & obj.valueelse:raise ValueError("Must be an instance of Test class")# Driver's codeif __name__ == "__main__":a = Test(9) # 0000 1001b = Test(12) # 0000 1100print(a & b)
Test
class. It has a magic method __and__()
to perform the bit-wise logical AND
operation.isinstance(obj, Test)
will check whether the argument object obj
is of the Test.class
type or not. We then perform the bitwise logical AND
Test
class, a
and b
. We then use the above-created function to perform logical AND
.OR
operatorBelow, we have an example of bitwise OR
operator's overloading.
# Python program to demonstrate# operator overloadingclass Test():def __init__(self, value):self.value = value# magic function to perform logical ORdef __or__(self, obj):print("Overloaded OR Operator")if isinstance(obj, Test):return self.value | obj.valueelse:raise ValueError("Must be an instance of Test class")# Driver's codeif __name__ == "__main__":a = Test(9) # 0000 1001b = Test(12) # 0000 1100print(a | b)
Test
class. It has the method __or__()
to perform the bitwise logical OR
operation.isinstance(obj, Test)
will check whether the argument object obj
is of Test
class type or not. We then perform the bitwise logical OR
.Test
class, a
and b
. Finally, we use the above-created function to perform the logical OR
.XOR
operatorBelow, we see an example of bitwise XOR
operator's overloading:
# Python program to demonstrate# operator overloadingclass Test():def __init__(self, value):self.value = value# magic function to perform logical XORdef __xor__(self, obj):print("Overloaded XOR Operator")if isinstance(obj, Test):return self.value ^ obj.valueelse:raise ValueError("Must be an instance of Test class")# Driver's codeif __name__ == "__main__":a = Test(9) # 0000 1001b = Test(12)# 0000 1100print(a ^ b)
Test
class. It has a method __xor__()
to perform the bitwise logical XOR
operation.isinstance(obj, Test)
will check whether the argument object obj
is of the Test.class
type or not. It then performs the bitwise logical XOR
.Test
class, that is, a=9
and b=12
. Finally, using the above created function, we perform the logical XOR
.NOT
operatorBelow, we see an example of bitwise NOT
operator's overloading:
# Python program to demonstrate# operator overloadingclass Test():def __init__(self, value):self.value = value# magic method for NOTdef __invert__(self):print("Overloaded NOT Operator")if isinstance(self, Test):return ~self.valueelse:raise ValueError("Must be an instance of Test class")# Driver's codeif __name__ == "__main__":a = Test(9) # 0000 1001print(~a)
Test
class with value
as a field. It has a magic method __invert__()
to perform the bitwise logical NOT
.isinstance(obj, Test)
will check whether the argument object obj
is of Test.class
type or not. We then perform the bitwise logical NOT
.Test
class a
. Finally, using the function we created above, we perform the logical NOT
.<<
Below, we see an example of the bitwise left shift (<<
) operator's overloading:
# Python program to demonstrate# operator overloadingclass Test():def __init__(self, value):self.value = value# Left shift of bitsdef __lshift__(self, obj):print("Overloaded LEFT SHIFT Operator")if isinstance(obj, Test):return self.value << obj.valueelse:raise ValueError("Must be an instance of Test class")# Driver's codeif __name__ == "__main__":a = Test(2) # 0000 0010b = Test(3) # 0000 0011print(a << b)
Test
class with value
as a field. It has a magic method __lshift__()
to perform the bitwise left shift.isinstance(obj, Test)
will check whether the argument object obj
is of Test.class
type or not. We then perform the bitwise logical left shift.Test
class types a=2
and b=3
. Finally, using the function we created above, we perform the logical left shift.>>
Below, we see an example of the bitwise right shift >>
operator's overloading:
# Python program to demonstrate# operator overloadingclass Test():def __init__(self, value):self.value = value# right shiftdef __rshift__(self, obj):print("Overloaded RIGHT SHIFT Operator")if isinstance(obj, Test):return self.value >> obj.valueelse:raise ValueError("Must be an instance of Test class")# Driver's codeif __name__ == "__main__":a = Test(2) # 0000 0010b = Test(3) # 0000 0011print(a >> b)
Test
class that contains a single member field. It has a magic method __rshift__()
to perform bitwise's right shift.isinstance(obj, Test)
will check whether the argument object obj
is of the Test.class
type or not. We then perform the bitwise logical right shift.Test
class types. a=2
and b=3
. Finally, we use the function we created above to perform the bitwise right shift.