What does the caret (^) operator do?
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Industries in Orbit Looping
--
Chapters
00:00 Question
00:55 Accepted answer (Score 248)
01:31 Answer 2 (Score 51)
01:50 Answer 3 (Score 15)
02:07 Answer 4 (Score 11)
03:18 Thank you
--
Full question
https://stackoverflow.com/questions/2451...
Accepted answer links:
[XOR]: http://en.wikipedia.org/wiki/Exclusive_o...
Answer 3 links:
[chapter 5 of the Python Language Reference]: https://docs.python.org/2/reference/expr...
Answer 4 links:
[infix]: https://en.wikipedia.org/wiki/Infix_nota...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #operators #caret
#avk47
ACCEPTED ANSWER
Score 266
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)
ANSWER 2
Score 52
It invokes the __xor__() or __rxor__() method of the object as needed, which for integer types does a bitwise exclusive-or.
ANSWER 3
Score 17
It's a bit-by-bit exclusive-or. Binary bitwise operators are documented in chapter 5 of the Python Language Reference.
ANSWER 4
Score 13
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