Question
How can I turn any bit on in a bit string using the OR operator?
Answer
The simplest way to think about using a mask and OR
ing to turn bits on is this:
Put a 1
in your mask wherever you want the bit to be turned on in the result. For example, if our mask is 0b1101
, the only possible place in the result that can be 0
is the second bit, or index 1, depending on what the input is. The other positions, indexes 0, 2, and 3, must be turned on in the result since OR
only requires one of the two inputs to be True
, or 1
.