私は次のコードを持っています。単純なビット操作を行うために使用されます。x = 12、y = 1の間にx&y == 0が成立しないのはなぜですか?
int x = 12; // (00...01100)
int y = 1;
int result;
result = x & y;
printf("x is %d\n", x); // 12
printf("y is %d\n", y); // 1
printf("result is %d\n", result); // 0
printf("x & y is %d\n", x & y); // 0
printf("!(x & y) is %d\n", !(x & y)); // 1
printf("x & y == 0 is %d\n", x & y == 0); // 0 why not true?
printf("result == 0 is %d\n", result == 0); // 1
printf("size of x & y is %d\n", sizeof(x & y)); // 4
printf("size of result is %d\n", sizeof(result)); // 4
なぜx & y == 0は真ではありませんか?ここ
ほとんどのコンパイラは、警告レベルをアップして、ここで何が起こっているのかを説明する警告を表示します。私は最大限の警告設定を有効にしてコンパイルを検討しています。 – templatetypedef
言語にもよりますが、ほとんどの場合、==と&の相対的な優先順位は愚かです(つまり、一般的な使用のために括弧が必要であり、珍しい用途ではありません)。しかしそれは普遍的ではありません。 – harold