Absolutely basic Java question which I'm having a hard time finding on Google. What does the following mean:
(7 & 8) == 0?
Is that equivalent to writing:
7 == 0 || 8 == 0?
I wrote a quick main which tests this, and it seems to be the case. I just wanted to make sure I'm not missing anything.
Nope. &
is bitwise and. It sets a bit if the corresponding bits are set in both inputs. Since in binary, 7 is 111
and 8 is 1000
, they have no bits in common, so the result is 0
.
There isn't really any shorthand syntax for the thing you suggest, not on a single line. There are a few workarounds -- test for membership in a Set
or BitSet
, use a switch
statement -- but nothing that's both as efficient and as short as just 7 == 0 || 8 == 0
.