Why do the following if comparisons show different results?
unsigned int i = 0;
unsigned int test = 0;
void setup() {
Serial.begin(115200);
for ( i=0; i<4; i++ ) {
Serial.println( i&3 );
test = i&3;
if ( i&3==0 ) {
Serial.println( "zero" ); // not show
}
if ( test==0 ) {
Serial.println( "test is zero" ); // show
}
}
}
void loop() {
delay(10);
}
/* Serial print results:
0
test is zero
1
2
3
*/
Do you not have your Compiler Warnings (in Preferences...) turned up to "All"? If you did so you would have gotten the helpful message:
sketch_oct09a.ino: In function 'void setup()':
sketch_oct09a.ino:10:16: warning: suggest parentheses around comparison in operand of '&' [-Wparentheses]
if ( i & 3 == 0 )
~~^~~~
That means "The '3 == 0' comparison is one side of your 'i & something' expression and that's often a mistake. You should put parens around the '3 == 0' part, to let people know you really mean it."
The referred Table says that the "equality operator" has the higher precedence. If so, will the Compiler attempt to evaluate "3 == 0" or will just ignore with an error message?