Menu Close

Python Bitwise Operators

Hello Python Programmer, I hope you are doing well. Today, In this article, we are going to learn all about the Python bitwise operator and its type with the help of examples.

In our previous Python tutorials, we have seen all about Python operators and their different types along with examples.

Python Bitwise Operators

Bitwise operator in Python are used to perform operators on integers. First, integer converted into binary and then operation perform on bit by bit, hence the name is bitwise operator.

Bitwise Operators in Python

There are 6 type of bitwise operators available in Python.

  • Bitwise AND
  • Bitwise OR
  • Bitwise XOR
  • Bitwise NOT
  • Bitwise Left shift
  • Bitwise Right shift

Bitwise Operators in Python

OperatorDescriptionSyntax
&Bitwise ANDx & y
|Bitwise ORx | y
^Bitwise XORx ^ y
~Bitwise NOT~x
<<Bitwise Left shiftx << y
>>Bitwise Right shiftx >> y

Bitwise AND Operator

Python Bitwise AND operator return 1 if the both the bit is 1 otherwise return 0.

Example


a = 10 # 1010 ( Binary Value )
b = 8 # 1000 ( Binary Value )

result = a & b
print('The result is:- ', result)

Explanation

a & b =   1010
      = + 1000
      =   1000 ( Decimal value of 1000 binary is 8)

Bitwise OR Operator

Python Bitwise OR operator return 1 if one of the bit is 1 otherwise return 0.

Example


a = 10 # 1010 ( Binary Value )
b = 8 # 1000 ( Binary Value )

result = a | b
print('The result is:- ', result)

Explanation

a | b =   1010
      =   1000
      =   1010 ( Decimal value of 1010 binary is 10)

Bitwise NOT Operator

Python bitwise NOT operator return the one’s complement of the bit.

Example


a = 10 # 1010 ( Binary Value )

print("Without one's complement, a is:- ", a)
print("One's complement of a is:- ", ~a)

Explanation

a  =  10 # 1010
~a = ~1010
   =  1010 + 1
   =  1011 ( Decimal value will be:- 11)

Bitwise XOR Operator

Python Bitwise XOR operator return 1 if one of the bit is 1 otherwise return 0.

Example

a = 10 # 1010 ( Binary Value )
b = 8 # 1000 ( Binary Value )

result = a | b
print('The result is:- ', result)

Explanation

a ^ b   =   1010
        =   1000
        =   1010 ( Decimal value of 1010 binary is 10)

Python bitwise left shift operator

In left shift operator <<, the left operand specifies the numbers and the right operand specifies the number to be shifted left. Zero bits are added to the right and access bits from the left are discarded.

Example


a = 8 ( Binary is 1000 )
n = 2 ( Number to be shifted )

result = a << n
print("The resut is:- ", result)

Python bitwise right shift operator

In left shift operator >>, the left operand specifies the numbers and the right operand specifies the number to be shifted right. Zero bits are added to the right and access bits from the left are discarded.

Example


a = 8 ( Binary is 1000 )
n = 2 ( Number to be shifted )

result = a >> n
print("The resut is:- ", result)

Python Bitwise Operator Overloading

So, In above section we have seen all about Python bitwise operator and all its type with the help of of examples. Do you know? How above Bitwise operators working. Let’s see some Python built-in function to perform operator overloading.

Here we explain operator overloading for custom objects.

Example: Operator Overloading


class Custom:
    num = 0
    def __init__(self, i):
        self.num = i
    
    def __and__(self, other):
        print("Bitwise and operator overloaded")
        if isinstance(other, Custom):
            return self.num & other.num
            
            
    def __or__(self, other):
        print("Bitwise or operator overloaded")
        if isinstance(other, Custom):
            return self.num | other.num
            
            
    def __xor__(self, other):
        print("Bitwise xor operator overloaded")
        if isinstance(other, Custom):
            return self.num ^ other.num
            
            
            
    def __invert__(self):
        print("Bitwise invert operator overloaded")
        return ~self.num
        
        
    def __lshift__(self, other):
        print("Bitwise left shift operator overloaded")
        if isinstance(other, int):
            return self.num << other
            
            
    def __rshift__(self, other):
        print("Bitwise right shift operator overloaded")
        if isinstance(other, int):
            return self.num >> other
            
            
if __name__=="__main__":
    a = Custom(10)
    b = Custom(12)
    
    print(a & b)
    print(a | b)
    print(a ^ b)
    print(~a)
    print(a << 2)
    print(a >> 2)

Output

 Bitwise and operator overloaded
 8
 Bitwise or operator overloaded
 14
 Bitwise xor operator overloaded
 6
 Bitwise invert operator overloaded
 -11
 Bitwise left shift operator overloaded
 40
 Bitwise right shift operator overloaded
 2

Python’s function starts and ends with a double underscore ( __ ) is called the magic or dunder method.
Click here to learn more about the Python Dunder method or magic method.

Conclusion

So, In this Python tutorial, we have seen all about Python bitwise operators along with the examples and also we have seen bitwise operator overloading using examples.

Bitwise operator in Python first convert integer to binary format and then perform an operation on that binary number bit by bit, Hence it is called bitwise operator.

I hope this article will have helped you. If you like this article, please share and support us, so that we come back with interesting Python tutorials.

Operators in Python


Reference:- Click Here

Thanks for reading

Python Assignment Operators
Python Logical Operators

Related Posts