This forum is now read-only. Please use our new forums at

1373 points
Submitted by
Haopei Yang
over 5 years ago

I don't quite understand the Shift Bitwise operators >> and <<

Hi everyone,

I don't understand how these two shift operators work. I'd be thankful for any help.

shift_right = 0b1100
shift_left = 0b1

shift_right = shift_right >> 2
shift_left = shift_left << 2

print bin(shift_right) #0b11
print bin(shift_left)   #0b100

25 votes


Remember that little trick we all taught in primary school when it comes to multiplying by 10, 100, 1000 etc. We shifted the numbers to the left by 1,2,3 decimal places. Dividing by 10, 100, 1000 etc was a rightward shift by 1,2 or 3.

So a bitwise shift to the left is like multiplying your binary number by 2,4,8,16 etc depending on how many bits you shifted it to the left. A bitwise shift to the right is a like a floor division by 2,4,8,16 etc depending on the number of bits you shifted you number to the right.

shift_left was 1(in binary and decimal) you shifted it two places to the left, multiply by 4, 4 in binary is 100

shift_right was 1100(12 in decimal units) you shifted it two places to the right, division by 4 which gives you 3 in decimal units) 3 in binary form is 11

375 points
Submitted by
Fred M'ule
over 5 years ago


7e68b3e24e2e03bcc35993d35851b87f?s=140&d=retro HatBat over 5 years ago

Good comment ; )

55df5d67e39efed12f0004ed_562413832 Trishna Guha almost 5 years ago

Thanks a lot for making me understand :)

Ada37f496f0a03631281bccce0413366?s=140&d=retro Schweicen over 4 years ago

Great answer, but why mathematically it's FLOOR division in this case?

19b55eed252ac0302acbf2105ab2b659?s=140&d=retro Victor Arjona over 4 years ago

Schweicen, I'm sure someone else will be able to answer this better than I can but I believe it has to do with the fact that it only deals with integers. For example, 14.8 as an integer in Python is read as 14 only. It does not round up, it only takes into consideration the integer in the number. Hope this helps and IF I'm wrong someone please correct me as I'd like to know if I'm right or learn a little from this as well. Good day!

7 votes


Note the position of the binary digits before and after the shifts.

0b1100 shifted by two bits to the right moves the 0s off the end and the two 1s to the right-most positions.

0b1 shifted by two bits to the left moves the 1 to the left by two places, and must fill in the two right-most bits with 0s.

17427 points
Submitted by
Glenn Richard
over 5 years ago


5201146dabf8219bb9004333_770067810 kp_569 over 5 years ago

What I do not understand is what is the whole purpose of this?... Any practical reason that we need to go through all these binary exercises?

52b5a89c9c4e9dd379001923_430674606 songofsongs over 5 years ago

Same page. I understand how the operators work but not how can this possibly be used practically? Why would we multiply with 2,4,16, etc using these? Is is that much faster than math operations?

64f439c01a818cec88b537462516b52b?s=140&d=retro KuroSetsuna29 over 5 years ago

As mentioned, bitwise operators are not used in most practical programming applications, however it is nice to at least recognize them when you see them as they are not very hard to understand what they do. There is also a very small niche of programming that uses bitwise operators extensively, most of which deal with hardware and low-level programming.

-1 votes


Oh...thats how the code works! It sure is nice of codeacadamy to provide a decent example for this problem. Silly me, I figured that it would work by something like: slideright(>>2)
or even slide
right = (>>2)
but no, once again, the instruction fail completely to define the actual problem. It sure is a good thing that there is such a huge database of help forms.

308 points
Submitted by
over 5 years ago

1 Comment

54ece12c86f552a203000160_24897876 sowen1 over 4 years ago