For some reason in C , the expressions if(!(n & 1)) and if(n & 1 == 0) seem to not be equivalent.
Can someone please explain why this happens?
CodePudding user response:
if(!(n & 1))will evaluate totrueif the least significant bit ofnis1.if(n & 1 == 0)is equivalent toif(n & (1 == 0)), which will becomeif (n & 0), which is alwaysfalse.
Check out the operator precedence table, you will see that == precedes &.
CodePudding user response:
Because of operator precedence. n & 1 == 0 is parsed as n & (1 == 0), not (n & 1) == 0.
