脱字号 (^) 运算符的作用是什么?

发布于 2024-08-25 10:09:52 字数 391 浏览 4 评论 0原文

我今天在 python 中遇到了插入符运算符并尝试了一下,得到了以下输出:

>>> 8^3
11
>>> 8^4
12
>>> 8^1
9
>>> 8^0
8
>>> 7^1
6
>>> 7^2
5
>>> 7^7
0
>>> 7^8
15
>>> 9^1
8
>>> 16^1
17
>>> 15^1
14
>>>

它似乎基于 8,所以我猜测是某种字节操作?我似乎找不到太多关于这个搜索网站的信息,除了它对浮点数的行为很奇怪之外,有人有这个运算符的链接吗?或者你能在这里解释一下吗?

I ran across the caret operator in python today and trying it out, I got the following output:

>>> 8^3
11
>>> 8^4
12
>>> 8^1
9
>>> 8^0
8
>>> 7^1
6
>>> 7^2
5
>>> 7^7
0
>>> 7^8
15
>>> 9^1
8
>>> 16^1
17
>>> 15^1
14
>>>

It seems to be based on 8, so I'm guessing some sort of byte operation? I can't seem to find much about this searching sites other than it behaves oddly for floats, does anybody have a link to what this operator does or can you explain it here?

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

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

发布评论

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

评论(5

酒儿 2024-09-01 10:09:53

一般来说,符号 ^infix 版本>__xor____rxor__ 方法。无论放置在符号右侧和左侧的数据类型都必须以兼容的方式实现此功能。对于整数,这是常见的 XOR 运算,但例如,对于 float 类型和 int 类型,没有内置的函数定义>:

In [12]: 3 ^ 4
Out[12]: 7

In [13]: 3.3 ^ 4
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-13-858cc886783d> in <module>()
----> 1 3.3 ^ 4

TypeError: unsupported operand type(s) for ^: 'float' and 'int'

Python 的一个巧妙之处是您可以在自己的类中重写此行为。例如,在某些语言中,^ 符号表示求幂。您可以通过这种方式做到这一点,就像一个例子:

class Foo(float):
    def __xor__(self, other):
        return self ** other

然后这样的事情就会起作用,现在,仅对于 Foo 的实例^ 符号表示求幂。

In [16]: x = Foo(3)

In [17]: x
Out[17]: 3.0

In [18]: x ^ 4
Out[18]: 81.0

Generally speaking, the symbol ^ is an infix version of the __xor__ or __rxor__ methods. Whatever data types are placed to the right and left of the symbol must implement this function in a compatible way. For integers, it is the common XOR operation, but for example there is not a built-in definition of the function for type float with type int:

In [12]: 3 ^ 4
Out[12]: 7

In [13]: 3.3 ^ 4
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-13-858cc886783d> in <module>()
----> 1 3.3 ^ 4

TypeError: unsupported operand type(s) for ^: 'float' and 'int'

One neat thing about Python is that you can override this behavior in a class of your own. For example, in some languages the ^ symbol means exponentiation. You could do that this way, just as one example:

class Foo(float):
    def __xor__(self, other):
        return self ** other

Then something like this will work, and now, for instances of Foo only, the ^ symbol will mean exponentiation.

In [16]: x = Foo(3)

In [17]: x
Out[17]: 3.0

In [18]: x ^ 4
Out[18]: 81.0
阳光①夏 2024-09-01 10:09:53

当您使用 ^ 运算符时,方法 __xor__ 被调用。

a^b 相当于a.__xor__(b)

此外,a ^= b 相当于 a = a.__ixor__(b)(其中 __xor__ 用作 __ixor__ 通过使用 ^= 隐式调用,但不存在)。

原则上,__xor__ 所做的事情完全取决于它的实现。 Python 中的常见用例有:

  • 集合的对称差异(所有元素恰好出现在两个集合之一中)

演示:

>>> a = {1, 2, 3}
>>> b = {1, 4, 5}
>>> a^b
{2, 3, 4, 5}
>>> a.symmetric_difference(b)
{2, 3, 4, 5}
  • 两个整数的位的按位不等于

演示:

>>> a = 5
>>> b = 6
>>> a^b
3

解释:

    101 (5 decimal)
XOR 110 (6 decimal)
-------------------
    011 (3 decimal)

When you use the ^ operator, behind the curtains the method __xor__ is called.

a^b is equivalent to a.__xor__(b).

Also, a ^= b is equivalent to a = a.__ixor__(b) (where __xor__ is used as a fallback when __ixor__ is implicitly called via using ^= but does not exist).

In principle, what __xor__ does is completely up to its implementation. Common use cases in Python are:

  • Symmetric Difference of sets (all elements present in exactly one of two sets)

Demo:

>>> a = {1, 2, 3}
>>> b = {1, 4, 5}
>>> a^b
{2, 3, 4, 5}
>>> a.symmetric_difference(b)
{2, 3, 4, 5}
  • Bitwise Non-Equal for the bits of two integers

Demo:

>>> a = 5
>>> b = 6
>>> a^b
3

Explanation:

    101 (5 decimal)
XOR 110 (6 decimal)
-------------------
    011 (3 decimal)
愿与i 2024-09-01 10:09:52

它是按位XOR(异或)。

当且仅当其参数不同(一个是 True,另一个是 False)时,它的计算结果为 True

演示:

>>> 0^0
0
>>> 1^1
0
>>> 1^0
1
>>> 0^1
1

解释你自己的一个例子:

>>> 8^3
11

这样想一下:

1000  # 8 (binary)
0011  # 3 (binary)
----  # APPLY XOR ('vertically')
1011  # result = 11 (binary)

It's a bitwise XOR (exclusive OR).

It evaluates to True if and only if its arguments differ (one is True, the other is False).

To demonstrate:

>>> 0^0
0
>>> 1^1
0
>>> 1^0
1
>>> 0^1
1

To explain one of your own examples:

>>> 8^3
11

Think about it this way:

1000  # 8 (binary)
0011  # 3 (binary)
----  # APPLY XOR ('vertically')
1011  # result = 11 (binary)
蹲墙角沉默 2024-09-01 10:09:52

它根据需要调用对象的 __xor__() 或 __rxor__() 方法,该方法对于整数类型执行按位异或。

It invokes the __xor__() or __rxor__() method of the object as needed, which for integer types does a bitwise exclusive-or.

三人与歌 2024-09-01 10:09:52

这是一个逐位异或。二元按位运算符记录在Python 语言参考的第 5 章。

It's a bit-by-bit exclusive-or. Binary bitwise operators are documented in chapter 5 of the Python Language Reference.

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