我注意到我可以用2 << 5来得到64,用1000 >> 2来得到250。

我也可以在打印中使用>>:

print >>obj, "Hello world"

这里发生了什么?


当前回答

在您的示例中,>>操作符用于两个不同的目的。在c++术语中,这个操作符是重载的。在第一个例子中,它被用作位运算符(右移),

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

而在第二个场景中,它用于输出重定向。你可以用它来处理文件对象,就像下面这个例子:

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

>>的第二次使用只适用于Python 2。在Python 3中,可以使用file=参数重定向print()的输出:

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

其他回答

<< 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

它们是存在于许多主流编程语言中的位移位运算符,<<是左移,>>是右移,它们可以演示如下表,假设一个整数只占用内存1字节。

| 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 |

2 << 5(左移)

向左移动2(二进制)5位。(向右注入0)

bin(16) # '0b10'

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

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

1000 >> 2(右移)

向右移动1000(二进制)2位。(往左注入0)

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

12 is less than < 2 48

当我们执行上面的语句时,12的实际二进制值是“00 1100”。左移(左移2位)返回值48,其二进制值是“11 0000”。

48 >> 2 12

48的二进制值是“11 0000”,执行上述语句右移(右移2位)后返回值12,其二进制值是“00 1100”。

是“按位”操作符。 https://wiki.python.org/moin/BitwiseOperators

>>> help("symbols")

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

X << y 返回x,其中的位向左移动了y位(右边的新位为零)。这就等于x乘以2**y。

X >> y 返回x,位向右移动y位。这和//'ing x * 2* y是一样的。

PD:在python 3.9中,应用于字典的操作符“|”合并字典。

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'}