- If you want to maintain state information in a status variable, then
choose an integral data type of sufficient size, and then instantiate a
pattern such as the following:
unsigned char status variable name;
- If you want to set a particular bit in a status variable,
then combine the status variable with the corresponding power-of-2
integer using the bitwise or operator,
|
, and then assign the status
variable to the result:
status variable = status variable | power of 2;
- If you want to test a particular bit in a status variable,
then combine the status variable with the corresponding
power-of-2 integer using the bitwise and operator,
&
,
and then test the resulting value in an if
statement.
if (status variable & power of 2)
...
- Whenever you use the bitwise-and operator,
&
, you can
treat one operand as a mask that lets through some bits of the other
operand and that filters out others.
- If you want to test several bits in a status variable to see whether any are
set, then combine the status variable with an appropriate multibit mask.