如何在优先级较高的操作之前在 C 中运行优先级较低的操作

时间:2021-05-11 02:16:32

标签: c operator-precedence

#include <stdio.h>

int main()
{
    short int a,b;
    a=1;
    b=1;
    if ( (a | 65534)&1 == (b | 65534)&1 )
    {
      printf("The rightmost bit is the same");  
    }
    else
    {
        printf("The rightmost bit is different");
    }
    return 0;
}


}

输出: 最右边的位不同

预期: 最右边的位是一样的

这里的“==”在“&”之前运行,这是不可取的。我可以使用另一个变量来解决这个问题,但不使用另一个变量是这个分配的重点......

1 个答案:

答案 0 :(得分:1)

添加更多括号:

if ( ((a & 65534)&1) == ((b & 65534)&1) )