I had both of these conditions inside an if statement to check if x was even or odd, but the !(x&1) seemed to execute the body of the if in case of an even x, while x&1==0 didn't execute it.
I expected both to give 0 considering 1 & 0 is 0 and 1 in 32 or 64 bit representations will be 000..01 and if, say, x is something like 10010101100 (even), then their bit-wise and should yield 0. Hence, I'm still not sure why !(x&1) works. Please correct me if I am wrong in anywhere.
Thank you.
It's just an operator precedence issue that you're facing.
In the expression
x&1 == 0, the==operator has the highest precedence; it will be evaluated before the&operator. As a consequence,x&1 == 0will evaluate tox & (1 == 0), which evaluates tox & 0(always equal to 0).You can find a reliable C++ Operator Precedence table on cppreference.com.
You can use parentheses to solve your issue:
(x & 1) == 0