Search code examples
coperator-precedence

How to run operations with lower precedence in C before ones with higher 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;
}


}

Output: The rightmost bit is different

Expected: The rightmost bit is the same

Here the "==" is run before "&" which is not desirable. I can take another variable to fix this but not taking another variable is sort of the point of this assignment...


Solution

  • Add more parenthesis:

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