So I understand bitwise AND is not always safe to use especially if the operands are going to numbers. But am trying to understand what is that tiny subtle reason behind this statement returns to False.
1110010110001000 == 1110010110001000 & 1 == 1 & 1000001000001101 == 1000001000001101 & 1 == 1
--> False
however when I use logical AND operator it returns True which is fairly verbose and clear but trying to understand why the first statement returns False.
and 1110010110001000 == 1110010110001000 and 1 == 1 and 1000001000001101 == 1000001000001101 and 1 == 1
--> True
PS: wasn't able to find any similar QA in my search.
&
has higher precedence than ==
.
So this:
1110010110001000 == 1110010110001000 & 1 == 1 & 1000001000001101 == 1000001000001101 & 1 == 1
means
1110010110001000 == (1110010110001000 & 1) == (1 & 1000001000001101) == (1000001000001101 & 1) == 1
which means
1110010110001000 == 0 == 1 == 1 == 1
which means (due to operator chaining)
(1110010110001000 == 0) and (0 == 1) and (1 == 1) and (1 == 1)
which is false.
In summary, don't use &
to mean logical and
.