36

What does the >> operator do? For example, what does the following operation 10 >> 1 = 5 do?

1
  • 3
    And this stuff is really useful for example in such way you can faster multiply 2 by some big numbers.If you want to multiply 2 by 16 (2 * 16 = 2 * 2^4) all you need to do is to shift 4 bits to the left. 2 * 16 == 2 << 4 == 32 Commented Aug 21, 2013 at 9:05

5 Answers 5

56

It's the right bit shift operator, 'moves' all bits once to the right.

10 in binary is

1010

shifted to the right it turns to

0101

which is 5

Sign up to request clarification or add additional context in comments.

4 Comments

note: in this example it moves them once because of the '1' on the right side of the bit shift operator.
Does this mean right shift halves the number if shifted to right ?
@Kishan: No. This one happens to halve it because it's a shift of one on a number that is alternating 1 and 0. Do some tests with numbers that you convert to binary. You'll see the correlation.
Well... a right shift does half the number for each step, but it rounds down. (Pretty much the same as "moving a decimal point to the left in a decimal number means dividing by 10", but you lose what is after point, rounding down). ------- A left shft, on the other hand, means doubling (compare to moving a decimal point to the right: means multiply by 10)
23

>> and << are the Right-Shift and Left-Shift bit-operators, i.e., they alter the binary representation of the number (it can be used on other data structures as well, but Python doesn't implement that). They are defined for a class by __rshift__(self, shift) and __lshift__(self, shift).

Example:

>>> bin(10) # 10 in binary
1010
>>> 10 >> 1 # Shifting all the bits to the right and discarding the rightmost one
5 
>>> bin(_) # 5 in binary - you can see the transformation clearly now
0101 

>>> 10 >> 2 # Shifting all the bits right by two and discarding the two-rightmost ones
2
>>> bin(_)
0010

Shortcut: Just to perform an integer division (i.e., discard the remainder, in Python you'd implement it as //) on a number by 2 raised to the number of bits you were shifting.

>>> def rshift(no, shift = 1):
...     return no // 2**shift
...     # This func will now be equivalent to >> operator.
...

3 Comments

Note that this is not a good idea with respect to performance. >> is O(1) in shift, while no // 2**shift is O(n) in shift. gist.github.com/anonymous/6807431
@lumbric Obviously, as not only do bit-shifts work slightly differently internally but they also have the advantage of being implemented in C. I only gave that algorithm to give an example of it and make how it works clear.
Yes, I thought so. I just wanted to clarify this with my comment. Somebody who is new to programing, might prefer your example for real code without noticing the drawbacks.
6

You can actually overloads right-shift operation(>>) yourself.

>>> class wierd(str): 
... def __rshift__(self,other): 
... print self, 'followed by', other 
... 
>>> foo = wierd('foo') 
>>> bar = wierd('bar') 
>>> foo>>bar 
foo followed by bar 

Reference: http://www.gossamer-threads.com/lists/python/python/122384

Comments

4

Its the right shift operator.

10 in binary is 1010 now >> 1 says to right shift by 1, effectively loosing the least significant bit to give 101, which is 5 represented in binary.

In effect it divides the number by 2.

1 Comment

When you say it divides by two, you mean two raised to the power of the second operator, in other words, a >> b == a / (2**b).
2

See section 5.7 Shifting Operations in the Python Reference Manual.

They shift the first argument to the left or right by the number of bits given by the second argument.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.