What does the caret operator (^) in Python do?

前端 未结 5 1711
忘了有多久
忘了有多久 2020-11-28 04:19

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
>         


        
相关标签:
5条回答
  • 2020-11-28 05:04

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

    0 讨论(0)
  • 2020-11-28 05:07

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

    0 讨论(0)
  • 2020-11-28 05:14

    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)
    
    0 讨论(0)
  • 2020-11-28 05:16

    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)
    
    0 讨论(0)
  • 2020-11-28 05:18

    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
    
    0 讨论(0)
提交回复
热议问题