educative.io

Logical operator - NOT

Hi, I have a question
When we use not we get the output as True :point_down:

my_bool = False
print(not my_bool)
we get the output here as True

Whereas why is the output -1 here? :point_down:

my_bool = False
print(~ my_bool)
we get the output as -1. What does it indicate ? Shouldn’t we get the same output as the previous one as not is represented by (~) .

The not operator returns the negation of a boolean expression, which means if the expression is True, then not will return False, and vice versa.

On the other hand, the tilde ~ operator is a bitwise NOT operator and performs bitwise negation. Therefore, ~False results in -1 because False is considered 0, and the bitwise NOT operator flips all the bits, making the result -1.

Happy learning at Educative.