The issue is that &
is not actually a logical "and", it's a bitwise operator. So it'll compare two numbers by each bit, and produce a number that has a bit set if both of the first two numbers had that bit set. So 1 & 2
will give you 0.
This wouldn't usually be a problem (True is 1, False is 0) - except that the operator precedence for & and and
relative to >
and =
are different.
So 1 == 1 & 2 > 0
(the first case):
- This is interpreted as
1 == (1 & 2) > 0
, or 1 == 0 > 0
, and since 1 == 0
is False the whole thing is False.
Whereas 1 == 1 and 2 > 0
(the second case):
- This is interpreted as
(1 == 1) and (2 > 0)
, and since both of those cases are True, the whole thing is True.