Question: Question:
Why does if 1 & 5
return True in Python, but if 2 & 5
does not return True? I couldn't find the answer even after thinking about it, so please let me know.
Thank you.
Answer: Answer:
In Python, & is a bitwise AND, so let's consider 1,2,5 as a binary number.
1 = 0001
2 = 0010
5 = 0101
Since it is an AND of bit operation, the result of the digit where both numbers of bits are set (1) is 1, and the other digits are 0.
1 & 5
0001
& 0101
0001
It will be.
Also, if it is 2 & 5
0010
& 0101
0000
It will be.
And since 0 is False and non-0 is True, 1 & 5 is True and 2 & 5 is False.