Why is the expression (0 == 0 & 1 == 1) evaluated as False?

Similarly (-1 == - 1 and 1 == 1) is also False.

Sorry if this is something obvious, but I can not find an explanation.

+6
source share
2 answers

&is a bitwise AND operator . As mentioned in the documentation , bitwise operators have higher priority than logical operators, therefore

0 == 0 & 1 == 1

becomes

0 == (0 & 1) == 1

And you can imagine that it goes down:

   0 == (0 & 1) == 1
=> 0 == 0 == 1
=> 0 == 0 and 0 == 1
=> True and False 
=> False

Assuming what you want is a logical AND , the python method for this would use and:

0 == 0 and 1 == 1

Which gives you True, as you expected.

+14

.

- . , , .

&.

0 & 1, 0.

0 == 0 == 1

0 1, False.

, python .

+5

All Articles