什么是>>和<< Python 中的运算符?
>>
和 <<
的用途是什么?
我在代码中读到了这个:
https://github.com/mlaiosa/emlx2maildir/blob /master/emlx2maildir.py
FL_READ = (1<<0)
FL_DELETED = (1<<1)
FL_ANSWERED = (1<<2)
FL_ENCRYPTED = (1<<3)
FL_FLAGGED = (1<<4)
FL_RECENT = (1<<5)
FL_DRAFT = (1<<6)
FL_INITIAL = (1<<7)
FL_FORWARDED = (1<<8)
FL_REDIRECTED = (1<<9)
FL_SIGNED = (1<<23)
FL_IS_JUNK = (1<<24)
FL_IS_NOT_JUNK = (1<<25)
FL_JUNK_LEVEL_RECORDED = (1<<29)
FL_HIGHLIGHT_IN_TOC = (1<<30)
我还找不到它的文档。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它是位移运算符。如果你有一个 1(
0b1
),并将其左移 4 位 (1 << 4
),你得到的是 0b10000,这意味着 16。这是文档:http://docs.python.org/reference/expressions.html#shifting-operations
It's bitshift operator. If you have a 1(
0b1
), and shift it left 4 bits(1 << 4
), what you get is 0b10000, which means 16.And here's the documentation: http://docs.python.org/reference/expressions.html#shifting-operations
这些运算符在 Python 语言参考的 第 5.7 节“移位操作”中定义< /a>:
The operators are defined in section 5.7, "Shifting Operations", of the Python Language Reference:
在大多数语言(包括 Python)中,这些都是移位运算符。它们对字节的位进行操作。
例如,8 是
0b00001000
。8>> 1
表示将其位向右移动 1 位,并在左侧添加一个零(0b00000100
或 4)。8>> 2
表示向右移动两次。 (0b00000010
或 2)。<<
是左移,其工作方式相反。 <代码>8 << 1 将输出为0b00010000
或 16。8 << 2
将输出为0b00100000
或 32。有关更多信息,请参阅 python 文档。
Python 2.x:http://docs.python.org/reference/expressions。 html#shifting-操作
Python 3.x:http://docs.python.org/py3k/参考/expressions.html#shifting-operations
In most languages, including Python, those are shift-operators. They work on the bits of a byte.
For example, 8 is
0b00001000
.8 >> 1
means shift the bits of it 1 digit to the right, adding a zero at the left (0b00000100
or 4).8 >> 2
means shift it to the right twice. (0b00000010
or 2). The<<
is a left shift, which works in the opposite way.8 << 1
would come out to0b00010000
or 16.8 << 2
would come out to0b00100000
or 32.See the python documentation for more information.
Python 2.x: http://docs.python.org/reference/expressions.html#shifting-operations
Python 3.x: http://docs.python.org/py3k/reference/expressions.html#shifting-operations