>> 和 << 在 Python 中是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22832615/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What do >> and << mean in Python?
提问by user3201152
I notice that I can do things like 2 << 5
to get 64 and 1000 >> 2
to get 250.
我注意到我可以做一些事情,比如2 << 5
得到 64 和1000 >> 2
250。
Also I can use >>
in print
:
我也可以>>
在print
:
print >>obj, "Hello world"
What is happening here?
这里发生了什么?
回答by James
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 位(右侧的新位为零)。这是一样的乘x
用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
.
返回x
位右移 y 位。这是相同的划分x
通过2**y
。
回答by doctorlove
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.
x << y 返回 x 的位向左移动 y 位(右侧的新位为零)。这与将 x 乘以 2**y 相同。
x >> y 返回 x 右移 y 位的位。这与 //'ing x by 2**y 相同。
回答by yosemite_k
I think it is important question and it is not answered yet (the OP seems to already know about shift operators). Let me try to answer, 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 bitwise operator (left shift), while in the second scenario it is merely used as output redirection. i.e.
我认为这是一个重要的问题,但尚未得到解答(OP 似乎已经了解移位运算符)。让我试着回答一下,您示例中的 >> 运算符用于两个不同的目的。在 C++ 术语中,此运算符是重载的。在第一个示例中,它用作按位运算符(左移),而在第二个场景中,它仅用作输出重定向。IE
2 << 5 # shift to left by 5 bits
2 >> 5 # shift to right by 5 bits
print >> obj, "Hello world" # redirect the output to obj,
example
例子
with open('foo.txt', 'w') as obj:
print >> obj, "Hello world" # hello world now saved in foo.txt
update:
更新:
In python 3 it is possible to give the file argument directly as follows:
在 python 3 中,可以直接给出 file 参数,如下所示:
print("Hello world", file=open("foo.txt", "a")) # hello world now saved in foo.txt
回答by chrstphrchvz
The other case involving print >>obj, "Hello World"
is the "print chevron" syntax for the print
statementin 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.
涉及的另一种情况print >>obj, "Hello World"
是Python 2 中print
语句的“print chevron”语法(在 Python 3 中删除,由函数的file
参数替换)。不是写入标准输出,而是将输出传递给方法。一个典型的例子是具有方法的文件对象。请参阅最近问题的答案:Python 中的双大于号。print()
obj.write()
write()
回答by PAC
12 << 2
48
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".
当我们执行上述语句时,实际二进制值 12 为“00 1100” 左移(左移 2 位)返回值 48,其二进制值为“11 0000”。
48 >> 2
12
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".
48 的二进制值为“11 0000”,执行上述语句后右移(右移 2 位)返回值 12,其二进制值为“00 1100”。
回答by Tsingyi
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.
它们是许多主流编程语言中都存在的移位运算符,<<
左移和>>
右移,它们可以如下表所示,假设一个整数在内存中只占用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 |