I am learning how to check the value of a bit. My textbook claims the following:
Even if bit 1 in
flagsis set to 1, the other bit setting inflagscan make the comparison untrue. Instead, you must first mask the other bits inflagsso that you compare only bit 1 offlagswithMASK:if ((flags & MASK) == MASK) puts("Wow!");
I'm having trouble understanding this concept.
For instance, let flags = 00001111 and MASK = 10110110.
Therefore, flags & MASK = 00000110.
If we now compared MASK and 00000110, we would be comparing bits 2 and 3. However, isn't the goal to compare the value of a specific (single) bit?
I must be misunderstanding this. What is the correct way to do this?