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
Answer 52930f9280ff33550c0001c7
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
4 comments
Good comment ; )
Thanks a lot for making me understand :)
Great answer, but why mathematically it’s FLOOR division in this case?
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!
Answer 523eeda5f10c60d1fe003e95
Note the position of the binary digits before and after the shifts.
0b1100
shifted by two bits to the right moves the 0
s off the end and the two 1
s 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 0
s.
3 comments
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?
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?
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.
Answer 52fafb08282ae39f72000c50
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: slide_right(>>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.
1 comments
hi
Popular free courses
- Free course
Learn SQL
In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.Beginner Friendly4 Lessons - Free course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner Friendly11 Lessons - Free course
Learn HTML
Start at the beginning by learning HTML basics — an important foundation for building and editing web pages.Beginner Friendly6 Lessons