什么是>>和<< Python 中的运算符?

发布于 2024-12-17 21:45:26 字数 721 浏览 5 评论 0 原文

>><< 的用途是什么?

我在代码中读到了这个:

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)

我还找不到它的文档。

What are >> and << for?

I read this in code:

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)

I cannot find the documentation of it yet.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我的鱼塘能养鲲 2024-12-24 21:45:26

它是位移运算符。如果你有一个 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

风月客 2024-12-24 21:45:26

这些运算符在 Python 语言参考的 第 5.7 节“移位操作”中定义< /a>:

这些运算符接受普通整数或长整数作为参数。参数被转换为通用类型。它们将第一个参数向左或向右移动第二个参数给出的位数。

右移n 位定义为除以pow(2, n)。左移 n 位定义为与 pow(2, n) 相乘。负移位计数会引发 ValueError 异常。

The operators are defined in section 5.7, "Shifting Operations", of the Python Language Reference:

These operators accept plain or long integers as arguments. The arguments are converted to a common type. They shift the first argument to the left or right by the number of bits given by the second argument.

A right shift by n bits is defined as division by pow(2, n). A left shift by n bits is defined as multiplication with pow(2, n). Negative shift counts raise a ValueError exception.

调妓 2024-12-24 21:45:26

在大多数语言(包括 Python)中,这些都是移位运算符。它们对字节的位进行操作。

例如,8 是0b000010008>> 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 to 0b00010000 or 16. 8 << 2 would come out to 0b00100000 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文