![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
Now you know that the powers of 2 correspond to single-bit-on numbers, and you know that the bitwise-or operator sets particular bits if either operand exhibits a 1 in the corresponding position. Accordingly, you know that, if you want to be sure that a particular bit in a status variable is set, you pick the power of 2 corresponding to that position, and then you use the bitwise-or operator to combine the value of the status variable with that power of 2.
For example, if you want to set bit 0 in the value of the status variable
to 1, you combine the status variable's value with the integer 1
,
the zeroth power of 2, using the bitwise-or operator. If you want to set bit 2 to
be 1, you combine the status variable's value with the integer 4
:
status = status | 1; <-- Sets bit 0 status = status | 4; <-- Sets bit 2