Hojin Cho
Hojin Cho

Reputation: 466

Why is `True and ~True` -2?

I'm terribly sorry if this is below-standard question, but I couldn't find clear answers.

As the title says, why does True and (~True) give -2 while True&(~True) gives 0? Also, Why do both of them not give False?

This is very counter-intuitive, since I expect for a single boolean expression & and and should work in the same way.

Upvotes: 3

Views: 696

Answers (2)

gilch
gilch

Reputation: 11651

True has a value of 1 in Python. Bit-inverting (~) a binary 1 (...0001) gives you ...1110. Since negative integers are represented by two's compliment, that's a value of -2.

Logical and returns its left operand if it's false, the right operand otherwise. (True is never false, obviously.)

Bitwise &, on the other hand, works on the individual bits. ...0001 & ...1110 have no 1 bits in the same position so they're all zeros in the result.


I was just surprised that a numpy array with dtype=bool acts differently with literal bool

Each Python type can implement an operator's methods with special method names. (Like .__invert__() for ~). But and, or, and not don't have these hooks, so often &, |, and ~ are used instead. For ints the implementation is bitwise, but other types can interpret operators however they want.

Note that bool is a subclass of int in Python, so it has the same operators as int. But you were operating on a numpy.ndarray, not on its individual components, so Python uses the ndarray implementation of the operators, which are not the same as bool when dtype=bool.

Upvotes: 9

user14248283
user14248283

Reputation:

Remember, True == 1 and False == 0.

So,

True and (~True)

Is the same as

1 and (~1)

~ is a bitwise operation. Which kind of inverts the binary of 1. If we

print(~1)

-2

So the whole thing is simplified into

1 and -2

Which is just -2


& vs and

I expect for a single boolean expression & and and should in the same way

Dont! and is a boolean test. & Is a bitwise operation. Both of them are very different.

Upvotes: 3

Related Questions