2 ms·
I just call the character a pipe, It's actually a "bitwise or". i think the reasoning is that it looks at each item in the two sets being compared and returns
by dec0dedab0de 5y ago
I just call the character a pipe, It's actually a "bitwise or". i think the reasoning is that it looks at each item in the two sets being compared and returns it if it is in either or both of the sets. Which functionally is the same thing as creating a union.
I usually avoid using bitwise operators because they get confusing, and the kind of stuff my team does rarely needs them. In this case I would have used union.
edit:
here are some of the docs
Bitwise operators:
https://wiki.python.org/moin/BitwiseOperators https://wiki.python.org/moin/BitwiseOperators
and how they work with sets:
https://docs.python.org/3.10/tutorial/datastructures.html#sets https://docs.python.org/3.10/tutorial/datastructures.html#se...
Plus the set docs have the methods with their corresponding operator:
https://docs.python.org/3.10/library/stdtypes.html#set https://docs.python.org/3.10/library/stdtypes.html#set
I can see how it would be nice and concise, but I think the methods are easier to understand.
- Snarwin 5y agoGiven two binary numbers a and b, the set of bits set to 1 in a|b is the union of the set of bits set to 1 in a and the set of bits set to 1 in b. Likewise for & (bitwise and) and intersection. So using bitwise operators to represent set operations like this is not too big of a stretch.