Reputation: 191
In C++, 4&1 == 0
, and 1&1 == 1
. However, 4&1 != 1&1
evaluates to 0
, instead of 1
, yet (4&1) != (1&1)
evaluates to 1
as expected. Why is this?
Upvotes: 5
Views: 125
Reputation: 14423
The relational operator !=
has a higher precedence than bitwise AND &
.
Thus the expression
4 & 1 != 1 & 1
will be parsed as
4 & (1 != 1) & 1
Upvotes: 6