Bitwise Operators in Python.

 


Bitwise Operators in Python.

In this post we will talk about Bitwise Operator in Python. Let's start..!

Bitwise operators works with bits or binary form of numbers. Here are some Bitwise Operator.


Here a table of logical operators in binary.



1} Bitwise NOT or Complement Operator ( ~ ) :

This bitwise operator simply does compliment. It means it reverse binary number for ex. ~ 1 = 0 and ~ 0 = 1 . Here is a number 16 if we write a bitwise not operator before 16 i.e. ~16 What will be the answer ? The answer will be -17 now Why answer is -17 ? How to calculate ? 

This very simple. First you have to convert given number into it's equivalent binary form. Then apply 1's complement on the binary form. 

For ex. Our number is 16 it's binary form is 10000 . now apply 1's complement i.e. 01111. ( simply reverse the number if 1 write 0 and if 0 then write 1. this how 1's complement works ). Final result is 01111 but Why we got -17. Because it will find a number who's 2's complement is same as our result and will return that number. If your input number is positive then output number will be negative and if your input number is negative then your output number will be positive. 

Here our number is 16 ( as input ) So number ( as output ) will be negative. And binary 1's complement of number 16 ( as input ) is 01111 and this is equal to 2's complement of number 17. And here our input number is positive so our result will be negative. That's why we got -17 as a output.





2} Bitwise AND ( & ) :

This operator will perform logical ' and ' operation on the binary bits of given number. It means only if  both bits are 1 then answer will be 1 otherwise your answer will be 0. for ex. We have two numbers 14 and 15. What will be the answer ? The answer will be 14 . Let's calculate and verify the answer.

Here two numbers are 14 and 15 their binary forms are 14 = 1110 and 15 = 1111. Now perform logical ' and ' operation on each and every bit. So 1110  &  1111 the result will be 1110 ( this is 14 number in decimal ). And this is binary of number 14 that's why we got answer as 14. 



 


3} Bitwise OR ( | ) :

This operator will perform logical ' or ' operation on the binary bits of given number. It means if  any one bit is 1 then answer will be 1 otherwise your answer will be 0. In this case we will get answer as 0 only when there both bits are 0. for ex. We have two numbers 5 and 3. What will be the answer ? The answer will be 7 . Let's calculate and verify the answer.

Here two numbers are 5 and 3 their binary forms are 5 =  0101 and 3 = 0011. Now perform logical ' or ' operation on each and every bit. So 0101  |  0011 the result will be 0111 ( this is 7 number in decimal ). And this is binary of number 7 that's why we got answer as 7.




4} Bitwise XOR ( ^ ) :

This operator will perform logical ' xor ' operation on the binary bits of given number. It means only if both bits is 1 or 0 then answer will be 0 otherwise your answer will be 1. In this case we will get answer as 1 only when there both bits are 0 and 1 or 1 and 0. Simply in ' xor ' operation for same bits answer will be 0 and for different bits answer will be 1. for ex. We have two numbers 3 and 9. What will be the answer ? The answer will be 10 . Let's calculate and verify the answer.

Here two numbers are 3 and 9 their binary forms are 3 = 0011 and 9 = 1001. Now perform logical ' xor ' operation on each and every bit. So 0011  ^  1001 the result will be 1010 ( this is 10 number in decimal ). And this is binary of number 10 that's why we got answer as 10. 




5} Left Shift ( << ) :

This operator will perform operation of bit shifting. It means it will add 0's in last of binary form. But How many 0's we have to add ? That is also simple just we have to specify the number ( for  left-shifting or adding more 0's in last ). for ex. we have a number 10 and we want to left-shift two bits i.e. 10 << 2 . What will be the result ? The result should be 40.  Let's calculate and verify the answer.

Here we have a number 10 and we want to left-shift this number by 2 bits. Means 10 << 2. To calculate this first we have to convert given number into it's equivalent binary form. So 10 = 1010 and we are left-shifting two bits in last ( simply adding two 0's in last ) So result will be 101000 ( this is the binary form of number 40 ) it means this is number 40 in decimal. That's why we got answer as 40. 





6} Right Shift ( >> ) :

This operator will also perform a bit shifting operation but in different way. It means this right-shift removes the actual bits from last in binary form. Here also we have to specify the number for how many bit will be right-shift ( simply removes from last  ) from actual binary form. We have a number 20 and we want to right-shift this number by 2 bits. i.e. 20 >> 2 . What will be the result ? The result should be 5. Let's calculate and verify the answer. 

Here we have a number 20 and we are right-shifting this number by 2 bits. Means 20 >> 2. To calculate this first we have to convert given number to it's equivalent binary form.  So 20 = 10100 and we are right-shifting two bits from last ( simply removes two bits from last ) So result will be 101 ( this is the binary form of number 5 ) it means this is number 5 in decimal. That's why we got answer as 5. 




This is all about Bitwise Operators in Python. Try your own examples with this operators. Take any random numbers and try all operators with them.


If you have any doubts in this post then please ask me in comment section below. I will try to solve it.


If you like these post and you have learned something from this then share this post with your friends and family.

Keep learning always.....!!! 😇😇😇




Previous Post  Next Post