Bitwise operations in Python

In this article, we will learn the bitwise operations in Python. In Python, the bitwise operations can be done only for integers.

The bitwise operator converts the given values into binary values and performs the operation bit by bit and returns the output as a decimal value.

In Python, there are six types of bitwise operations which are listed below:

  1. Bitwise AND ( & )
  2. Bitwise OR ( | )
  3. Bitwise NOT ( ~ )
  4. Bitwise XOR ( ^ )
  5. Bitwise right shift ( >> )
  6. Bitwise left shift ( << )

Now let’s see each operator briefly

Bitwise AND ( & )

Bitwise AND operator converts the given two values as binary values and then checks each bit separately. If both bits are 1. It returns 1, else it returns 0.

Truth Table
A B Output
0 0 0
0 1 0
1 0 0
1 1 1

Example:

a=5
b=6
print(a&b)

Output:

4

In the above example the code is executed as explained below:

a = 5   ->    101(Binary)
b = 6   ->  & 110(Binary)
            = 100
            = 4(Decimal)

Bitwise OR ( | )

The Bitwise OR operator converts the given two values as binary values and then checks each bit separately. If both bits are 0. It returns 0, else it returns 1.

Truth Table
A B Output
0 0 0
0 1 1
1 0 1
1 1 1

Example:

a=5
b=6
print(a|b)

Output:

7

In the above example the code is executed as explained below:

a = 5  ->    101(Binary) 
b = 6  ->  | 110(Binary) 
           = 111 
           = 7(Decimal)

Bitwise NOT ( ~ )

Bitwise NOT operator returns the output as the 1’s complement of the given value.

Example:

a=5
print(~a)

Output:

-6

In the above example the code is executed as explained below:

a = 5  ->    101(Binary)
           ~ 101
           = -(101 + 1)
           = -(110)
           = -6(Decimal)

Bitwise XOR ( ^ )

Bitwise XOR operator converts the given two values as binary values and then checks each bit separately. If both bits are same. It returns 0, else it returns 1.

Truth Table
A B Output
0 0 0
0 1 1
1 0 1
1 1 0

Example:

a=5
b=6
print(a^b)

Output:

3

In the above example the code is executed as explained below:

a = 5  ->    101(Binary) 
b = 6  ->  ^ 110(Binary) 
           = 011 
           = 3(Decimal)

Bitwise Right shift ( >> )

Bitwise Right shift operator shifts the binary value of the given value by given no of bits to the right and return it as a decimal value.

Example:

a=5
print(a>>2)

Output:

1

In the above example the code is executed as explained below:

a = 5   ->     101(Binary)
            >> 101
            =  001
            =  1(Decimal)

Bitwise Left shift ( >> )

Bitwise Left shift operator shifts the binary value of the given value by given no of bits to the left and return it as a decimal value.

Example:

a=5
print(a<<2)

Output:

20

In the above example the code is executed as explained below:

a = 5   ->     101(Binary)
            >> 101
            =  10100
            =  20(Decimal)

 

I hope you learned about the Bitwise operations in Python from the above article.

Leave a Reply

Your email address will not be published. Required fields are marked *