I am learning how to check the value of a bit. My textbook claims the following:
Even if bit 1 in
flags
is set to 1, the other bit setting inflags
can make the comparison untrue. Instead, you must first mask the other bits inflags
so that you compare only bit 1 offlags
withMASK
: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?