8.8. Operator Bitwise
a & b
- anda | b
- ora ^ b
- xora << b
- lshifta >> b
- rshifta &= b
- ianda |= b
- iora ^= b
- ixora <<= b
- ilshifta >>= b
- irshift
Operator |
Method |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
Talk - Sebastiaan Zeeff: Demystifying Python's Internals: Diving into CPython by implementing... https://www.youtube.com/watch?v=HYKGZunmF50
Łukasz Langa - Life Is Better Painted Black, or: How to Stop Worrying and Embrace Auto-Formatting https://www.youtube.com/watch?v=esZLCuWs_2Y
https://docs.influxdata.com/influxdb/v2.0/query-data/get-started/query-influxdb/
>>> 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'])