How Bitwise Operators work

Let’s start the tutorial from where we stopped at last week. As I promised last week, Today I will give you an explanation on “How Bitwise Operators work”. Let’s take the same example variables that we took in last week.

a=70                
b=35
Bitwise AND Operator

If you use Bitwise AND Operator for above variables in your program as follows,

print (a&b)

The answer will be 2.

Let me explain the reason for that. When we do Bitwise Operations in our programs, operands that we use in the Bitwise Operations are taken as binary values. That means, variable a is taken as 01000110and also the variable b is taken as 001000112

And the Bitwise AND operator (&) will perform Logical AND on each and every corresponding bits of the binary values of operands which we are using in the operation.

Logical AND behaves as follows

  • 0 AND 0 equals to 0
  • 0 AND 1 equals to 0
  • 1 AND 0 equals to 0
  • 1 AND 1 equals to 1
01000110 (Binary Value of a)
|||||||| (Perform Logical AND)
00100011 (Binary Value of b)
________
00000010 (Answer)
========

If we convert the answer to decimal, we will get 2 as the result.

Bitwise OR Operator
print (a|b)

The answer will be 103.

Bitwise OR operator (|) will perform Logical OR on each and every corresponding bits of the binary values of operands which we are using in the operation.

Logical OR behaves as follows

  • 0 OR 0 equals to 0
  • 0 OR 1 equals to 1
  • 1 OR 0 equals to 1
  • 1 OR 1 equals to 1
01000110 (Binary Value of a)
|||||||| (Perform Logical OR)
00100011 (Binary Value of b)
________
01100111 (Answer)
========

Let’s convert the answer to decimal, we will get 103 as the result.

Bitwise XOR Operator
print (a^b)

The answer will be 101.

Bitwise XOR operator (^) will perform Logical XOR on each and every corresponding bits of the binary values of operands which we are using in the operation.

Logical XOR behaves as follows

  • 0 XOR 0 equals to 0
  • 0 XOR 1 equals to 1
  • 1 XOR 0 equals to 1
  • 1 XOR 1 equals to 0
01000110 (Binary Value of a)
|||||||| (Perform Logical XOR)
00100011 (Binary Value of b)
________
01100101 (Answer)
========

Let’s convert the answer to decimal, we will get 101 as the result.

Negation Operator
print (~a)

The answer will be -71.

Negation operator (~) will perform Logical Negation on each bit of the binary value which we are using.

Logical Negation behaves as follows

  • NOT 0 equals to 1
  • NOT 1 equals to 0
01000110 (Binary Value of a)
|||||||| (Perform Logical Negation)
________
10111001 (Answer)
========

The answer is received in the two’s complement form. So the result after converting the above answer to decimal is -71.

Left Shift

eg 1:

print (a<<3)

The answer will be 560.

The Left Shift Operator (<<) will add zeros (in this case, 3 zeros will be added) to the end of the binary value of the left operand .

01000110    (Binary Value of a)
01000110000 (After adding 3 zeros to the end of the Binary Value)
___________
01000110000 (Answer)
===========

After converting the above answer to decimal, we will get the result, 560.

eg 2:

print (a<<1)

The answer will be 140.

01000110  (Binary Value of a)
010001100 (After adding 1 zero to the end of the Binary Value)
_________
010001100 (Answer)
=========

After converting the above answer to decimal, we will get the result, 140.

Right Shift

eg 1:

print (a>>3)

The answer will be 8.

The Right Shift Operator (>>) will add zeros (in this case, 3 zeros will be added) to the beginning of the binary value of the left operand and, number of digits which is equal to the number zeros added, will be removed from the end of the binary value (we added 3 zeros to the beginning of the binary value, that means we should remove 3 digits from the end).

01000110     (Binary Value of a)
00001000110  (After adding 3 zeros to the beginning of the Binary Value)
00001000     (After removing 3 digits from the end of the Binary Value)
____________
00001000     (Answer)
============

After converting the above answer to decimal, we will get the result, 8.

eg 2:

print (a>>1)

The answer will be 35.

01000110  (Binary Value of a)
001000110 (After adding 1 zero to the beginning of the Binary Value)
00100011  (After removing 1 digit from the end of the Binary Value)
_________
01000110  (Answer)
=========

After converting the above answer to decimal, we will get the result, 35.

Ok. that’s it.  Let’s meet next week with another python tutorial. Do not hesitate to use the comment area below. Put your suggestions and ask your questions there. And If you think this tutorial is valuable, please share this with your friends. As well as do not forget to subscribe us.

Leave a Reply

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