[英]What does the caret (^) operator do?
I ran across the caret operator in python today and trying it out, I got the following output:我今天在 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
>>>
It seems to be based on 8, so I'm guessing some sort of byte operation?它似乎基于8,所以我猜测某种字节操作? 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?
我似乎找不到很多关于这个搜索网站的信息,除了它对浮动的行为很奇怪,有人有这个操作员做什么的链接吗?或者你能在这里解释一下吗?
It's a bitwise XOR (exclusive OR).这是一个按位异或(异或)。
It results to true if one (and only one) of the operands (evaluates to) true.如果一个(并且只有一个)操作数(计算结果)为真,则结果为真。
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)
它根据需要调用对象的__xor__()
或__rxor__()
方法,对于整数类型执行按位异或。
It's a bit-by-bit exclusive-or.这是一个一点一点的异或。 Binary bitwise operators are documented in chapter 5 of the Python Language Reference .
二元按位运算符记录在Python Language Reference 的第 5 章中。
Generally speaking, the symbol ^
is an infix version of the __xor__
or __rxor__
methods.一般来说,符号
^
是__xor__
或__rxor__
方法的中缀版本。 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
:对于整数,它是常见的
XOR
运算,但例如,对于类型为int
float
类型的函数没有内置定义:
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. Python 的一个优点是您可以在自己的类中覆盖此行为。 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.然后像这样的事情会起作用,现在,对于
Foo
only 的实例, ^
符号将意味着取幂。
In [16]: x = Foo(3)
In [17]: x
Out[17]: 3.0
In [18]: x ^ 4
Out[18]: 81.0
When you use the ^
operator, behind the curtains the method __xor__
is called.当您使用
^
运算符时,会在幕后调用方法__xor__
。
a^b
is equivalent to a.__xor__(b)
. a^b
相当于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).此外,
a ^= b
等效于a = a.__ixor__(b)
(其中__xor__
用作通过使用^=
隐式调用__ixor__
但不存在时的后备)。
In principle, what __xor__
does is completely up to its implementation.原则上,
__xor__
所做的完全取决于它的实现。 Common use cases in Python are: Python 中的常见用例是:
Demo:演示:
>>> a = {1, 2, 3}
>>> b = {1, 4, 5}
>>> a^b
{2, 3, 4, 5}
>>> a.symmetric_difference(b)
{2, 3, 4, 5}
Demo:演示:
>>> a = 5
>>> b = 6
>>> a^b
3
Explanation:解释:
101 (5 decimal)
XOR 110 (6 decimal)
-------------------
011 (3 decimal)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.