Reputation: 349
Why is x & 0
different from x & ~1
?
Shouldn't ~1
and 0
be the same since 0
is the complement of ~1
?
x = 21
print(f"x = {bin(x)}")
print(f"x & 0 = {bin(x & 0)}")
print(f"x & ~1 = {bin(x & ~1)}")
results in
x = 0b10101
x & 0 = 0b0
x & ~1 = 0b10100
Upvotes: 1
Views: 93
Reputation: 1
~
and &
are bitwise operations, not logical operations. The logical expression "x and False" indeed yields False, as does "not True":
print x and False
print x and not True
Upvotes: 0