Python Bitwise Operators

Bitwise Operators

The bitwise operator is only applicable for int and boolean datatypes. When we try to use the bitwise operators on other datatypes then we immediately get an error.

  • &
  • |
  • ~
  • ^
  • <<
  • >>

Some valid combinations are:

Example:

  • 4 & 5 

This is a valid expression as four and five both our int.

  • True & True

Both the values are true which is the boolean type, thus the expression is valid.

  • 10.5 & 2.6

This expression is invalid as 10.5 and 2.6 have floating point which is unsupported type in Python.


  1. & (and):

Since we have applied the bitwise operators and then if both the bits are 1 then the output will be one otherwise the output will be zero.

Example:>>>4 & 5

 Output:4

In the above example if we compare both the numbers bit by bit then we will get the output as 4. First, we convert 4 and 5 to binary i.e. 100 and 101. Comparing the last bits of both numbers we have 0 and 1 and since both the bits are not 1 then we get a 0. Then we compare the second last digits of both numbers i.e. 0 and 0 which will return 0. Then the first bit i.e. 1 and 1 which returns 1. Combining all the returned outputs together we have 100 i.e. 4, which will be the output.


  1. | (or):

The ‘or’ bitwise operators give 1 as output when at least one of the bits are 1 otherwise there will be zero as output.

Example:>>>4 | 5

 Output:4

For this example, if we compare both the numbers bit by bit then we will get the output as 5. First, we convert 4 and 5 to binary i.e. 100 and 101. Comparing the last bits of both numbers we have 0 and 1 and since one of the bits are 1 then we get 1. Then we compare the second last digits of both numbers i.e. 0 and 0 which will return 0. Then the first bit i.e. 1 and 1 which returns 1. Combining all the returned outputs together we have 101 i.e. 5, as the output.


  1. ^ (xor): 

This is known as the bitwise xor Operator. It returns one if both the bits are different from one another otherwise it written zero.

Example:>>>4 ^ 5

 Output:1

In the above code we will compare both the numbers. First we convert 4 and 5 to binary which is 100 and 101 respectively. Comparing the last bits of both numbers we have 0 and 1 and since both bits are different we get 1. Then we compare the second last digits of both numbers i.e. 0 and 0 which will return 0 since both are same. Then the first bit i.e. 1 and 1 which returns 0. Combining all the returned outputs together we have 001 i.e. 1.


  1. ~ (complement operator):

The bitwise complement operator inverts the given value of the argument. If the given value is one then it will change it to 0 and vice versa. 

Example:>>> ~4

Output:-5

At the memory level, the number 4 will be represented as 00000000…100. Now the first bit i.e. the sign bit is considered to decide the sign of the number. The positive numbers are represented via 1’s complement whereas the -ve numbers are represented via the 2’s complement. Thus now the bits get inverted as 111111111…011. Now the first bit is 1 i.e. it is a negative number which will be represented by 2’s complement and returns the value -5 as an answer.

Example:>>> ~True

Output:-2


  1. << (left shift):

As the name suggests the left shift operator is used to shift the bits certain places to the left. The blank spaces generated are known as right-hand vacant cells and are filled with 0’s.

Example: >>>print(10<<2)

Output:40

The example above can be understood by the above diagram. The number 10 in binary is shifted 2 places to the left and the blank spaces are replaced with zero. The numbers from 0 to 6 represent the powers of 2 and powers at 1 are added. Here 2 raised to 5 and 2 raised to three are added to get the output 40.

Example:>>>print(True<<2)

Output:4

In the above example, we use the left shift operator on boolean value true. This results in 4 because true is represented as 0001. On shifting this two places to the left we get 0100 which is 4.


  1. << (right shift):

The left shift operator is used to shift the bits certain places to the right. The blank spaces generated are known as right vacant cells and are replaced with the sign bit.

In sign bits the +ve numbers are represented by 0 and -ve numbers are represented by 1.

Example: >>>print(10>>2)

Output:2

In the above example due to the right shifting two places the last two bits of 1010 i.e. 10 are removed and we are only left with 10 i.e. 2.

Example:>>>print(True>>2)

Output:0

In this code we use the right shifting operator on boolean value true. This results in 0 because true is represented as 0001. On shifting this two places to the right the last two bits 0 and 1 gets removed and we are left with 0.

Leave a Comment

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