+ 1

The use of >> and << in python?

What exactly do these symbols mean? What would the output be of 7 >> 2 be compared to 7 >> 2 << 4? How does it work exactly? Are there other types of examples that use characters instead of integers?

12th Jan 2017, 9:34 PM
Briggs McKnight
Briggs McKnight - avatar
3 Answers
+ 8
off hand I don't know, other than maybe just "greater than/less than". However it annoys me on coding tutorial websites how they copy paste their text from the default ide: >>>>print("this is annoying") Since when you're trying to copy paste code from it, it'll mess up your code. if no one answers this just look it up
12th Jan 2017, 11:14 PM
Ahri Fox
Ahri Fox - avatar
+ 8
https://wiki.python.org/moin/BitwiseOperators something about shifting bits? I dunno. gl
12th Jan 2017, 11:19 PM
Ahri Fox
Ahri Fox - avatar
+ 4
Ahh! I think I understand. So basically Bitwise Operators work in binary. These specific Operators, << and >>, shift the binary code left or right respectively. To give an example with the numbers I gave, 7 = 0111, >> = shift to the right, and 2 is how many times i want to shift right. The result would be moving from 0111 to 0011 to 0001 (because i am moving 2 times), which gives you the output equaling 1. To clarify, with each move to the right, I am throwing away the information at one end of the binary code and adding zeros to the other end. If we were to replace the 2 with a 1, ie 7 >> 1, the resulting output would be 4 (0011). If we were to replace >> with <<, ie 7 << 2, the resulting output would be 28 (0001 1100). If we were to try my other example 7 >> 2 << 4, the resulting output would be 16 (0001 0000). To go a little further into detail with this one we are basically just moving right twice, giving us 1, and then with that 1 we are moving 4 times to the left, giving us 16. There are a couple problems in the challenges that use these two (<<, >>), so hopefully this helps explain them a bit!
13th Jan 2017, 4:02 AM
Briggs McKnight
Briggs McKnight - avatar