Double less than and double greater than signs.
双倍小于和双倍大于标志。
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 >>obj, "Hello world"
What is happening here?
这里发生了什么?
5 个解决方案
#1
30
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相同。
#2
14
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 ++术语中,此运算符已过载。在第一个示例中,它用作按位运算符(左移),而在第二个示例中,它仅用作输出重定向。即
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
#3
7
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.
涉及print >> obj,“Hello World”的另一个案例是Python 2中print语句的“print chevron”语法(在Python 3中删除,替换为print()函数的文件参数)。输出不是写入标准输出,而是传递给obj.write()方法。典型的例子是具有write()方法的文件对象。查看更近期问题的答案:Python中的双重大于号。
#4
4
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返回x,其中位向左移位y位(右侧的新位为零)。这与将x乘以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 by 2 ** y相同。
#5
2
12 << 2
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
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”。
#1
30
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相同。
#2
14
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 ++术语中,此运算符已过载。在第一个示例中,它用作按位运算符(左移),而在第二个示例中,它仅用作输出重定向。即
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
#3
7
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.
涉及print >> obj,“Hello World”的另一个案例是Python 2中print语句的“print chevron”语法(在Python 3中删除,替换为print()函数的文件参数)。输出不是写入标准输出,而是传递给obj.write()方法。典型的例子是具有write()方法的文件对象。查看更近期问题的答案:Python中的双重大于号。
#4
4
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返回x,其中位向左移位y位(右侧的新位为零)。这与将x乘以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 by 2 ** y相同。
#5
2
12 << 2
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
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”。