条件检查给出错误答案

-1 c bit-shift operator-precedence bitwise-and relational-operators

#include <stdio.h>
int main(){
  printf("%d,%d\n", 2 & (1<<1) , 2 & (1<<1)>0 );
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

该程序的输出是2,0.

2 & (1<<1)等于 2,大于 0。那么为什么2 & (1<<1) > 0计算结果为零?

Vla*_*cow 5

这个表情

2 & (1<<1)>0
Run Code Online (Sandbox Code Playgroud)

相当于

2 & ( (1<<1)>0 )
Run Code Online (Sandbox Code Playgroud)

由于运算符的优先级。即关系运算符 > 的优先级高于按位 AND 运算符。As1 << 1大于0则子表达式( ( 1 << 1 ) > 0 )产生值1

所以2 & 1产生,0因为在二进制中1可以表示(为了简单起见)像01and 2- 就像10and

 01
&
 10
---
 00
Run Code Online (Sandbox Code Playgroud)

看来你的意思是下面的表达

( 2 & ( 1 << 1 ) ) > 0
Run Code Online (Sandbox Code Playgroud)