msapere
msapere

Reputation: 191

why does AND in parenthesis evaluate differently than without?

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

Answers (1)

Brian61354270
Brian61354270

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

Related Questions