I have looked at Stack overflow answers for questions similar to mine, but I have not found answers that could address my question (here and here) or explain in a way that makes it clear to me. In trying to understand, I experimented as below:
def Even1(num): # I understand this and use it
if num % 2 == 0:
return num
def Even2(num):
return not (num & 1)
def Even3(num):
return not (num and 1)
filter(Even1, range(7)) ==> [2, 4, 6]
filter(Even2, range(7)) ==> [0, 2, 4, 6]
filter(Even3, range(7)) ==> [0]
1: not (4 & 1) = True
2: not (1 & 4) = True
3: not (4 & 2) = True
4: not (3 & 4) = True
5: not (4 & 3) = True
6: not (4 & 4) = False
7: not (3 & 1) = False
8: not (1 & 3) = False
9: not (3 & 2) = False
10: not (2 & 3) = False
11: not (3 & 3) = False
Based on #1 to #5 I thought any even number with any other number in a not (x & y)
arrangement evaluates to False (changed to True by not). I thought it had something to do with the fact that bin(x)
where x
is even ends in 0 and when odd ends in a 1. So maybe not
checks for the last bit. But #6 disproves that assumption. And also the order in which the numbers are presented
has no influence.
What does expression #6 have in common with expression #11 besides that each evalutes identical/equal numbers?
And based on #7 to #11 it looks like odd numbers in not (x & y)
where x and y both odd evaluate to True (changed to False by not). Would that be correct?
Lastly, Even2 and Even3: replace &
with and
. I understand that the first is bitwise, and the second logical, but what does that mean so that I end with the results.