educative.io

Educative

Bitwise Operation

Hi team,
Below is an excerpt from a Bitwise chapter in Python:

Let’s suppose we have a binary number 0110 (6 in decimal). The operation we perform is 0110 >> 2:

  • 0110 >> 2
  • 0011 (move one step to the right)
  • 0001 (move one more step to the right)
  • Operation complete

Similarly, we can move 0110 twice to the left with the following operation 0110 << 2:

  • 0110 << 2
  • 01100 (move one step to the left)
  • 011000 (move one more step to the left)
  • Operation complete

So , I understood the Bitwise right , where to keep the size of the binary value, it is getting filled up by zeroes, when shifting to its right. But here, for the case of Bitwise left, I cannot see that its getting shfted to its left ut the zeroes are getting added up. Please help on this.

Thanks and Regards
Ankan


Course: Educative: Interactive Courses for Software Developers
Lesson: Educative: Interactive Courses for Software Developers

Hi @Ankan_Chanda
Thank you for reaching out to us. In python, the bitwise left shift operator shifts the bits of the binary representation of the input number to the left side by a specified number of places. The empty bits created by shifting the bits are filled by 0s. The output of 0110 (6 in decimal) << 2 will be 011000 in binary which converts to the value 24 in integer format.
Here you can observe that we have shifted the bits by 2 places due to which the input number has been multiplied by 2^2 i.e. 4. Similarly, if we left shift the number by n bits, the integer value of the number will be multiplied by 2^n.
I hope this helps