#include <stdio.h>
int main() {
int a = 1;
int b = 0;
// Following two statements returns the same output
printf("\nhah %d", a != 0 & b != 0); // returns 0
printf("\nhah %d", a != 0 && b != 0); // returns 0
// Despite the common misconception that the statement 1 when written explicitly
// is this...
printf("\nmeh %d", (a != (0 & b)) != 0); // returns 1
// ..., statement 1's AND(&) operator still retain the same operator precedence as its short-circuit cousin(&&)
printf("\n");
const int ALT_KEY = 2;
int input = 1;
// should return 0, it returns 0:
printf("\nhah %d", (input & ALT_KEY) == ALT_KEY);
// despite the expectation that this "should" return 0, this doesn't return 0:
printf("\nmeh %d", input & ALT_KEY == ALT_KEY);
// So it means, despite the introduction of short-circuit operator,
// the non-short-circuit logical/bitwise operator (&,|) still retain their
// operator precedence.
// Hence, the unparenthesized expression (input & ALT_KEY == ALT_KEY), when written explicitly is still evaluated as:
printf("\nhah %d", input & (ALT_KEY == ALT_KEY) ); // returns 1
// Similar with operator precedence of logical operator:
printf("\nhah %d", input && ALT_KEY == ALT_KEY ); // returns 1
// Logical operator when written explicitly
printf("\nhah %d", input && (ALT_KEY == ALT_KEY) ); // returns 1
printf("\n");
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiAKaW50IG1haW4oKSB7CiAKIAogICAgaW50IGEgPSAxOwogICAgaW50IGIgPSAwOwogCiAgICAvLyBGb2xsb3dpbmcgdHdvIHN0YXRlbWVudHMgcmV0dXJucyB0aGUgc2FtZSBvdXRwdXQKICAgIHByaW50ZigiXG5oYWggJWQiLCBhICE9IDAgJiBiICE9IDApOyAvLyByZXR1cm5zIDAKICAgIHByaW50ZigiXG5oYWggJWQiLCBhICE9IDAgJiYgYiAhPSAwKTsgLy8gcmV0dXJucyAwCiAKICAgIC8vIERlc3BpdGUgdGhlIGNvbW1vbiBtaXNjb25jZXB0aW9uIHRoYXQgdGhlIHN0YXRlbWVudCAxIHdoZW4gd3JpdHRlbiBleHBsaWNpdGx5CiAgICAvLyBpcyB0aGlzLi4uCiAgICBwcmludGYoIlxubWVoICVkIiwg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