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