user13520: I'm learning about accessing ports directly on the ATMEGA 328 using an Arduino Uno. The program sets up pin 13 as an output and pin 2 as an input. I then enable the pull up resistor on pin 2. 
I read Pin 2 and set the LED high or low depending on if pin 2 is high or low. 
Why is it that I cannot seem to use == HIGH in my if statement instead of != LOW?
void setup() {
  MCUCR &= ~(1 << 4); //disables pull-up disable (redundant as default is off)
  DDRB |= (1 << 5); //sets pin 13 (B5 on ATMEGA 328) as output
  DDRD &= ~(1 << 2); //sets pin 2 (D2 on ATMEGA 328) as input (redundant as default is input)
  PORTD |= (1 << 2); //since it's an input sets pull-up resistor ON
  PORTB &= ~(1 << 5); //sets pin 13 (B5 on ATMEGA 328) as LOW

}

void loop() {
  if ((PIND & (1 << 2)) != LOW) PORTB |= (1 << 5); //sets pin 13 (B5 on ATMEGA 328) as HIGH
  else PORTB &= ~(1 << 5); //sets pin 13 (B5 on ATMEGA 328) as LOW
   delay(100);

}


user30005: if ((PIND & (1 << 2)) != LOW) PORTB |= (1 << 5); //sets pin 13 (B5 on ATMEGA 328) as HIGH
  else PORTB &= ~(1 << 5); //sets pin 13 (B5 on ATMEGA 328) as LOW

PIND & (1<<2) will return a value of either (1<<2) if PD.2 is high, or 0 if PD.2 is low.
So it basically says that if PD2 is high, set PB5; otherwise, clear PB5.enter code here

user15180: Because 
((PIND & (1 << 2)) != LOW)

means exactly
(PIND & 4) != 0

and that is true if PIND is 4, 5, 6, 7, 12, 13, ... (any number having 4 in it binary), so it yelds
4 != 0 

Which is definitively not equal
4 == 1


BTW: in C any integer != 0 is true and 0 is false, so you can as well write:
if (PIND & (1 << 2)) PORTB |= (1 << 5); //sets pin 13 (B5 on ATMEGA 328) as HIGH


13520: <p>I'm learning about accessing ports directly on the ATMEGA 328 using an Arduino Uno. The program sets up pin 13 as an output and pin 2 as an input. I then enable the pull up resistor on pin 2. </p>

<p>I read Pin 2 and set the LED high or low depending on if pin 2 is high or low. </p>

<p><strong>Why is it that I cannot seem to use == HIGH in my if statement instead of != LOW?</strong></p>

<pre><code>void setup() {
  MCUCR &amp;= ~(1 &lt;&lt; 4); //disables pull-up disable (redundant as default is off)
  DDRB |= (1 &lt;&lt; 5); //sets pin 13 (B5 on ATMEGA 328) as output
  DDRD &amp;= ~(1 &lt;&lt; 2); //sets pin 2 (D2 on ATMEGA 328) as input (redundant as default is input)
  PORTD |= (1 &lt;&lt; 2); //since it's an input sets pull-up resistor ON
  PORTB &amp;= ~(1 &lt;&lt; 5); //sets pin 13 (B5 on ATMEGA 328) as LOW

}

void loop() {
  if ((PIND &amp; (1 &lt;&lt; 2)) != LOW) PORTB |= (1 &lt;&lt; 5); //sets pin 13 (B5 on ATMEGA 328) as HIGH
  else PORTB &amp;= ~(1 &lt;&lt; 5); //sets pin 13 (B5 on ATMEGA 328) as LOW
   delay(100);

}
</code></pre>
 30005: <pre><code>if ((PIND &amp; (1 &lt;&lt; 2)) != LOW) PORTB |= (1 &lt;&lt; 5); //sets pin 13 (B5 on ATMEGA 328) as HIGH
  else PORTB &amp;= ~(1 &lt;&lt; 5); //sets pin 13 (B5 on ATMEGA 328) as LOW
</code></pre>

<p>PIND &amp; (1&lt;&lt;2) will return a value of either (1&lt;&lt;2) if PD.2 is high, or 0 if PD.2 is low.</p>

<p>So it basically says that if PD2 is high, set PB5; otherwise, clear PB5.<code>enter code here</code></p>
 15180: <p>Because </p>

<pre><code>((PIND &amp; (1 &lt;&lt; 2)) != LOW)
</code></pre>

<p>means exactly</p>

<pre><code>(PIND &amp; 4) != 0
</code></pre>

<p>and that is true if PIND is 4, 5, 6, 7, 12, 13, ... (any number having 4 in it binary), so it yelds</p>

<pre><code>4 != 0 
</code></pre>

<p>Which is definitively not equal</p>

<pre><code>4 == 1
</code></pre>

<hr>

<p>BTW: in C any integer != 0 is true and 0 is false, so you can as well write:</p>

<pre><code>if (PIND &amp; (1 &lt;&lt; 2)) PORTB |= (1 &lt;&lt; 5); //sets pin 13 (B5 on ATMEGA 328) as HIGH
</code></pre>
