user3201152
user3201152

Reputation: 2799

What do >> and << mean in Python?

I notice that I can do things like 2 << 5 to get 64 and 1000 >> 2 to get 250.

Also I can use >> in print:

print >>obj, "Hello world"

What is happening here?

Upvotes: 264

Views: 451644

Answers (10)

bl3ssedc0de
bl3ssedc0de

Reputation: 1005

Are "bitwise" operators. https://wiki.python.org/moin/BitwiseOperators

>>> help("symbols")

+-------------------------------------------------+---------------------------------------+
| Operator                                        | Description                           |
|=================================================|=======================================|
| "<<", ">>"                                      | Shifts                                |
+-------------------------------------------------+---------------------------------------+
| "&"                                             | Bitwise AND                           |
+-------------------------------------------------+---------------------------------------+
| "|"                                             | Bitwise OR                            |
+-------------------------------------------------+---------------------------------------+
| "~x"                                            | bitwise NOT                           |
+-----------------------------------------------------------------------------------------+
| "^"                                             | Bitwise XOR                           |
+-------------------------------------------------+---------------------------------------+


x >> y Returns x with the bits shifted to the right by y places. This is the same as //'ing x by 2**y.

128 >> 1

64 = 01000000

x << y Returns x with the bits shifted to the left by y places (and new bits on the right-hand-side are zeros). This is the same as multiplying x by 2**y.

64 << 1

128 = 10000000

PD: In python 3.9 the operator " | " Applied to dictionaries merges dictionaries.

https://docs.python.org/3.9/whatsnew/3.9.html

>>> x = {"key1": "value1 from x", "key2": "value2 from x"}
>>> y = {"key2": "value2 from y", "key3": "value3 from y"}
>>> x | y
{'key1': 'value1 from x', 'key2': 'value2 from y', 'key3': 'value3 from y'}
>>> y | x
{'key2': 'value2 from x', 'key3': 'value3 from y', 'key1': 'value1 from x'}

Upvotes: 9

Yonas Kassa
Yonas Kassa

Reputation: 3690

The >> operator in your example is used for two different purposes. In C++ terms, this operator is overloaded. In the first example, it is used as a bitwise operator (right shift),

2 << 5  # shift left by 5 bits
        # 0b10 -> 0b1000000
1000 >> 2  # shift right by 2 bits
           # 0b1111101000 -> 0b11111010

While in the second scenario it is used for output redirection. You use it with file objects, like this example:

with open('foo.txt', 'w') as f:
    print >>f, 'Hello world'  # "Hello world" now saved in foo.txt

This second use of >> only worked on Python 2. On Python 3 it is possible to redirect the output of print() using the file= argument:

with open('foo.txt', 'w') as f:
    print('Hello world', file=f)  # "Hello world" now saved in foo.txt

Upvotes: 166

amirhe
amirhe

Reputation: 2341

2 << 5 (shift to left)

Shifting 2 (in binary) 5 bits to the left. (inject zero to the right)

bin(16) # '0b10'

shifted = bin(2) + '0' * 5 # '0b1000000'

int(shifted, 2) # 64
2 << 5 # 64

1000 >> 2 (shift to right)

Shifting 1000 (in binary) 2 bits to the right. (inject zero to the left)

bin(1000) # '0b1111101000'

# add 00 to the left and remove last digit from the right
# '0b 00(add these bits) 11111010 00(remove these bits)'
shifted = '0b0011111010'

int(shifted, 2) # 250
1000 >> 2 # 250

Upvotes: 4

Nandish
Nandish

Reputation: 31

I verified the following on both Python 2.7 and Python 3.8

I did print(100<<3) Converting 100 to Binary gives 1100100. What I did is I droped the first 3 bits and added 3 bits with the value '0' at the end. So it should result as 0100000, and I converted this to Decimal and the answer was 32.

For my suprise when I executed print(100<<3) the answer was 800. I was puzzled. I converted 800 to Binary to check whats going on. And this is what I got 1100100000.

If you see how 800 was Python answer, they did not shift or drop the first 3 bits but they added value '0' to last 3 bits.

Where as print(100>>3) , worked perfect. I did manual calculation and cheked the print result from python. It worked correctly. Dropped last 3 bits and added value '0' to first 3 bits.

Looks like (100<<3) , left shift operator has a bug on Python.

Upvotes: 2

Mayank Awasthi
Mayank Awasthi

Reputation: 79

<< Mean any given number will be multiply by 2the power
for exp:- 2<<2=2*2'1=4
          6<<2'4=6*2*2*2*2*2=64

Upvotes: 0

PAC
PAC

Reputation: 1684

12 << 2

48

Actual binary value of 12 is "00 1100" when we execute the above statement Left shift ( 2 places shifted left) returns the value 48 its binary value is "11 0000".

48 >> 2

12

The binary value of 48 is "11 0000", after executing above statement Right shift ( 2 places shifted right) returns the value 12 its binary value is "00 1100".

Upvotes: 64

Tsingyi
Tsingyi

Reputation: 799

They are bit shift operator which exists in many mainstream programming languages, << is the left shift and >> is the right shift, they can be demonstrated as the following table, assume an integer only take 1 byte in memory.

| operate | bit value | octal value |                       description                        |
| ------- | --------- | ----------- | -------------------------------------------------------- |
|         | 00000100  |           4 |                                                          |
| 4 << 2  | 00010000  |          16 | move all bits to left 2 bits, filled with 0 at the right |
| 16 >> 2 | 00000100  |           4 | move all bits to right 2 bits, filled with 0 at the left |

Upvotes: 40

chrstphrchvz
chrstphrchvz

Reputation: 727

The other case involving print >>obj, "Hello World" is the "print chevron" syntax for the print statement in Python 2 (removed in Python 3, replaced by the file argument of the print() function). Instead of writing to standard output, the output is passed to the obj.write() method. A typical example would be file objects having a write() method. See the answer to a more recent question: Double greater-than sign in Python.

Upvotes: 15

doctorlove
doctorlove

Reputation: 19262

These are the shift operators

x << y Returns x with the bits shifted to the left by y places (and new bits on the right-hand-side are zeros). This is the same as multiplying x by 2**y.

x >> y Returns x with the bits shifted to the right by y places. This is the same as //'ing x by 2**y.

Upvotes: 10

James
James

Reputation: 25533

These are bitwise shift operators.

Quoting from the docs:

x << y

Returns x with the bits shifted to the left by y places (and new bits on the right-hand-side are zeros). This is the same as multiplying x by 2**y.

x >> y

Returns x with the bits shifted to the right by y places. This is the same as dividing x by 2**y.

Upvotes: 122

Related Questions