8.8. Operator Bitwise

  • a & b - and

  • a | b - or

  • a ^ b - xor

  • a << b - lshift

  • a >> b - rshift

  • a &= b - iand

  • a |= b - ior

  • a ^= b - ixor

  • a <<= b - ilshift

  • a >>= b - irshift

Table 8.6. Boolean Operator Overload

Operator

Method

obj & other

obj.__and__(other)

obj | other

obj.__or__(other)

obj ^ other

obj.__xor__(other)

obj << other

obj.__lshift__(other)

obj >> other

obj.__rshift__(other)

obj &= other

obj.__iand__(other)

obj |= other

obj.__ior__(other)

obj ^= other

obj.__ixor__(other)

obj <<= other

obj.__ilshift__(other)

obj >>= other

obj.__irshift__(other)

8.8.1. Syntax

>>> class MyClass:
...     def __and__(other): ...       # a & b
...     def __or__(other): ...        # a | b
...     def __xor__(other): ...       # a ^ b
...     def __lshift__(other): ...    # a << b
...     def __rshift__(other): ...    # a >> b
...
...     def __iand__(other): ...      # a &= b
...     def __ior__(other): ...       # a |= b
...     def __ixor__(other): ...      # a ^= b
...     def __ilshift__(other): ...   # a <<= b
...     def __irshift__(other): ...   # a >>= b

8.8.2. Example

>>> True + True
2
>>> True & True
True

8.8.3. AND - Conjunction

1 & 1 = 1
1 & 0 = 0
0 & 1 = 0
0 & 0 = 0
>>> True & True
True
>>>
>>> True & False
False
>>>
>>> False &  True
False
>>>
>>> False &  False
False

8.8.4. OR - Alternative

1 | 1 = 1
1 | 0 = 1
0 | 1 = 1
0 | 0 = 0
>>> True | True
True
>>>
>>> True | False
True
>>>
>>> False | True
True
>>>
>>> False | False
False

8.8.5. XOR - Exclusive Alternative

1 ^ 1 = 0
1 ^ 0 = 1
0 ^ 1 = 1
0 ^ 0 = 0
>>> True ^ True
False
>>>
>>> True ^ False
True
>>>
>>> False ^ True
True
>>>
>>> False ^ False
False

8.8.6. Bool

>>> a = True
>>> b = False
>>> a & b
False
>>> a ^ b
True
>>> a | b
True

8.8.7. Set

>>> a = {1,2,3}
>>> b = {2,3,4}
>>> a & b
{2, 3}
>>> a ^ b
{1, 4}
>>> a | b
{1, 2, 3, 4}
>>> a |= b
>>> print(a)
{1, 2, 3, 4}

8.8.8. Dict

>>> a = {'a': 1, 'b': 2}
>>> b = {'c': 3, 'd': 4}
>>>
>>> a | b
{'a': 1, 'b': 2, 'c': 3, 'd': 4}
>>>
>>> a |= b
>>> print(a)
{'a': 1, 'b': 2, 'c': 3, 'd': 4}

8.8.9. Operator

  • operator.and_(a, b) - and - a & b

  • operator.or_(a, b) - or - a | b

  • operator.xor(a, b) - xor - a ^ b

  • operator.lshift(a, b) - lshift - a << b

  • operator.rshift(a, b) - rshift - a >> b

  • operator.iand(a, b) - iand - a &= b

  • operator.ior(a, b) - ior - a |= b

  • operator.ixor(a, b) - ixor - a ^= b

  • operator.ilshift(a, b) - ilshift - a <<= b

  • operator.irshift(a, b) - irshift - a >>= b

>>> import operator
>>> operator.and_(2, 3)
2
>>> operator.or_(2, 3)
3
>>> operator.xor(2, 3)
1
>>> operator.lshift(2, 3)
16
>>> operator.rshift(2, 3)
0
>>> operator.iand(2, 3)
2
>>> operator.ior(2, 3)
3
>>> operator.ixor(2, 3)
1
>>> operator.ilshift(2, 3)
16
>>> operator.irshift(2, 3)
0

8.8.10. Use Case - 1

  • XOR as pow

  • Excel uses ^ to rise number to the power of a second number

>>> from dataclasses import dataclass
>>>
>>>
>>> @dataclass
... class Number:
...     value: int
...
...     def __xor__(self, other):
...         return Number(self.value ** other.value)
>>>
>>>
>>> a = Number(2)
>>> b = Number(4)
>>>
>>> a ^ b
Number(value=16)

8.8.11. Use Case - 2

  • Game

>>> hero >> Direction(left=10, up=20)  

8.8.12. Use Case - 3

  • Numpy

>>> import numpy as np
>>> a = np.array([[1, 2, 3],
...               [4, 5, 6],
...               [7, 8, 9]])
>>>
>>> a > 2
array([[False, False,  True],
       [ True,  True,  True],
       [ True,  True,  True]])
>>>
>>> (a>2) & (a<7)
array([[False, False,  True],
       [ True,  True,  True],
       [False, False, False]])
>>>
>>> (a>2) & (a<7) | (a>3)
array([[False, False,  True],
       [ True,  True,  True],
       [ True,  True,  True]])

Python understands this:

>>> ~( (a>2) & (a<7) | (a>3) )
array([[ True,  True, False],
       [False, False, False],
       [False, False, False]])

As as chained calls of the following methods:

>>> a.__gt__(2).__and__(a.__lt__(7)).__or__(a.__gt__(3)).__invert__()
array([[ True,  True, False],
       [False, False, False],
       [False, False, False]])

8.8.13. Use Case - 5

>>> def upper(text):
...     return str.upper(text)
>>>
>>> def lower(text):
...     return str.lower(text)
>>>
>>> def capitalize(text):
...     return str.capitalize(text)

Let's make a transformation:

>>> name = 'Mark Watney'
>>> upper(name)
'MARK WATNEY'

What if we have a pipe operator to do that?

>>> name = 'Mark Watney'
>>> name |> upper  
Traceback (most recent call last):
SyntaxError: invalid syntax

Why? Because we can chain multiple pipe operations:

>>> name = 'Mark Watney'
>>> name |> upper |> lower |> capitalize
Traceback (most recent call last):
SyntaxError: invalid syntax

8.8.14. Use Case - 6

>>> class Group:
...     def __init__(self, members=None):
...         self.members = members if members else []
...
...     def __repr__(self):
...         members = sorted(self.members)
...         return f'Group({members})'
...
...     def __or__(self, other):
...         members = self.members + other.members
...         return Group(members)
...
...     def __iadd__(self, user):
...         self.members.append(user)
...         return self
>>>
>>>
>>> users = Group()
>>> users += 'mwatney'
>>> users += 'avogel'
>>> users += 'cbeck'
>>>
>>> staff = Group()
>>> staff += 'rmartinez'
>>> staff += 'bjohanssen'
>>>
>>> admins = Group()
>>> admins += 'mlewis'
>>>
>>> everyone = users | staff | admins
>>> everyone
Group(['avogel', 'bjohanssen', 'cbeck', 'mlewis', 'mwatney', 'rmartinez'])