user46111: In the sketch I'm working on, I'm storing several 1-bit values representing different boolean states in a single byte, trying to save precious RAM.
(example for 5 boolean states: dec10 = b00001010, the five states are 0, 1, 0, 1, 0)
I'm then using an if statement to determine if the relevant bit is TRUE or FALSE:
if ((options>>3) & 1 == 1) 
The above will evaluate true when the relevant bit is 1, so if I want to carry out something in that case, great, no problem!  
However, if I want my if statement to evaluate TRUE in the event that the relevant bit is 0, I can't work out what code I need.

I thought the problem might be that the other 'non-relevant' bits were interfering, so I tried bit shifting the LSB all the way to MSB position and then shifting back to LSB:
if ((options<<4>>7) & 1 == 0)
but this still isn't doing what I'm trying to achieve (and seems like a lot of work for the processor).
It also feels like I've tried just about every combination of ~&0, !&0, ==0, !=0, ~&1, !&1, ==1, !=1 there is, but I must have missed something...  
Can anyone tell me how I get an if statement to evaluate true in the event that the bit is a zero?

user10794: There is an alternative to doing bit shifts, and that is to use the bit macro which is defined in Arduino.h (and thus is automatically available).
#define bit(b) (1UL << (b))

Now if you want to see if the second bit is set (which as a mask would be 0x04, being 1 shifted left 2 times) you can do this:
if (options & bit (2))
   {
   // bit number 2 is set
   }
else
   {
   // bit number 2 is clear
   }

This is easier to read than:
if (options & (1 << 2))

or:
if (options & 0x04)


And to invert it:
if (!(options & bit (2)))
   {
   // bit number 2 is clear
   }

Or:
if ((options & bit (2)) == 0)
   {
   // bit number 2 is clear
   }


Also, the various processor bits are given as bit numbers in the Atmega include files (rather than bit masks).

So, for example, to set some processor bits you can do this:
WDTCSR |= bit (WDCE) | bit (WDE);

That is easier to read than:
WDTCSR |= (1 << WDCE) |  (1 << WDE);

And much easier than turning them into hex numbers:
WDTCSR |= 0x10 | 0x08;

And in case you are wondering how to clear a bit, you and in the 1s complement:
WDTCSR &= ~bit (WDCE);


user4143: The simplest thing is just to mask an individual bit, not do any shifting:
if (options & 0x04) {
    // Do something if true
} else {
    // Do something if false
}

Or, to invert it:
if (!(options & 0x04)) {
    // Do something if false 
} else {
    // Do something if true.
}

The thing with C is that 0 is false, and anything else is true.  So if your options variable contains 0b00001010 and you AND it with 0x04 (which is HEX for 0b00001000) you end up with 0b00001000.  That is 0x04, which is something other than 0, so it's true.  
If options contains 0b00000010 and you AND it with 0x04, you get 0b00000000, which is 0. Since 0 is false the result is false.
You don't care what the actual number is - you only care if it's 0.

46111: <p>In the sketch I'm working on, I'm storing several 1-bit values representing different boolean states in a single byte, trying to save precious RAM.
(example for 5 boolean states: <code>dec10</code> = <code>b00001010</code>, the five states are <code>0</code>, <code>1</code>, <code>0</code>, <code>1</code>, <code>0</code>)</p>

<p>I'm then using an <code>if</code> statement to determine if the relevant bit is <code>TRUE</code> or <code>FALSE</code>:<br>
<code>if ((options&gt;&gt;3) &amp; 1 == 1)</code>  </p>

<p>The above will evaluate <code>true</code> when the relevant bit is <code>1</code>, so if I want to carry out something in that case, great, no problem!  </p>

<p><strong>However</strong>, if I want my <code>if</code> statement to evaluate <code>TRUE</code> in the event that the relevant bit is <code>0</code>, I can't work out what code I need.</p>

<hr>

<p>I thought the problem might be that the other 'non-relevant' bits were interfering, so I tried bit shifting the LSB all the way to MSB position and then shifting back to LSB:<br>
<code>if ((options&lt;&lt;4&gt;&gt;7) &amp; 1 == 0)</code><br>
but this still isn't doing what I'm trying to achieve (and seems like a lot of work for the processor).</p>

<p>It also feels like I've tried just about every combination of <code>~&amp;0</code>, <code>!&amp;0</code>, <code>==0</code>, <code>!=0</code>, <code>~&amp;1</code>, <code>!&amp;1</code>, <code>==1</code>, <code>!=1</code> there is, but I must have missed something...  </p>

<p>Can anyone tell me how I get an if statement to evaluate true in the event that the bit is a zero?</p>
 10794: <p>There is an alternative to doing bit shifts, and that is to use the <code>bit</code> macro which is defined in Arduino.h (and thus is automatically available).</p>

<pre><code>#define bit(b) (1UL &lt;&lt; (b))
</code></pre>

<p>Now if you want to see if the second bit is set (which as a mask would be 0x04, being 1 shifted left 2 times) you can do this:</p>

<pre><code>if (options &amp; bit (2))
   {
   // bit number 2 is set
   }
else
   {
   // bit number 2 is clear
   }
</code></pre>

<p>This is easier to read than:</p>

<pre><code>if (options &amp; (1 &lt;&lt; 2))
</code></pre>

<p>or:</p>

<pre><code>if (options &amp; 0x04)
</code></pre>

<hr>

<p>And to invert it:</p>

<pre><code>if (!(options &amp; bit (2)))
   {
   // bit number 2 is clear
   }
</code></pre>

<p>Or:</p>

<pre><code>if ((options &amp; bit (2)) == 0)
   {
   // bit number 2 is clear
   }
</code></pre>

<hr>

<p>Also, the various processor bits are given as bit numbers in the Atmega include files (rather than bit masks).</p>

<p><a href="https://i.stack.imgur.com/hrRdE.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/hrRdE.png" alt="WDTCSR register"></a></p>

<p>So, for example, to set some processor bits you can do this:</p>

<pre><code>WDTCSR |= bit (WDCE) | bit (WDE);
</code></pre>

<p>That is easier to read than:</p>

<pre><code>WDTCSR |= (1 &lt;&lt; WDCE) |  (1 &lt;&lt; WDE);
</code></pre>

<p>And much easier than turning them into hex numbers:</p>

<pre><code>WDTCSR |= 0x10 | 0x08;
</code></pre>

<p>And in case you are wondering how to clear a bit, you <em>and</em> in the 1s complement:</p>

<pre><code>WDTCSR &amp;= ~bit (WDCE);
</code></pre>
 4143: <p>The simplest thing is just to mask an individual bit, not do any shifting:</p>

<pre><code>if (options &amp; 0x04) {
    // Do something if true
} else {
    // Do something if false
}
</code></pre>

<p>Or, to invert it:</p>

<pre><code>if (!(options &amp; 0x04)) {
    // Do something if false 
} else {
    // Do something if true.
}
</code></pre>

<p>The thing with C is that 0 is false, and <em>anything else</em> is true.  So if your <code>options</code> variable contains <code>0b00001010</code> and you AND it with 0x04 (which is HEX for <code>0b00001000</code>) you end up with <code>0b00001000</code>.  That is 0x04, which is something other than 0, so it's true.  </p>

<p>If <code>options</code> contains <code>0b00000010</code> and you AND it with 0x04, you get <code>0b00000000</code>, which is 0. Since 0 is false the result is false.</p>

<p>You don't care what the actual number is - you only care if it's 0.</p>
