Reputation: 6321
I've a if
statement like this:
if ((x & y) == y && (x & z) == 0) { /* do something... */}
Is possible to simplify this if
statement removing the &&
operator and using only bitwise operators?
Upvotes: 2
Views: 915
Reputation: 5040
A short equivalent expression is
(x & (y ^ z)) == y
which is probably easiest to see correct by looking at all 8 results for a single bit (this is sufficient since the result for each bit is independent of all other bits).
Upvotes: 2