This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: ~(True) and ~(False) gives incorrect result
Type: Stage: resolved
Components: Versions:
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: eric.smith, nmadurkar, xmorel
Priority: normal Keywords:

Created on 2021-10-05 04:26 by nmadurkar, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (4)
msg403190 - (view) Author: Narendra Madurkar (nmadurkar) Date: 2021-10-05 04:26
~(True) returns -2
~(False) returns -1
msg403191 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2021-10-05 04:40
What were you expecting?

I think those are correct. See:
>>> ~1
-2
>>> ~0
-1
msg403192 - (view) Author: Narendra Madurkar (nmadurkar) Date: 2021-10-05 04:52
True is a boolean so ~True should return False according to me.
True is not the same as 1
msg403196 - (view) Author: Xavier Morel (xmorel) * Date: 2021-10-05 06:16
> True is a boolean so ~True should return False according to me.

That's be a BC break for no reason: if you want to invert a boolean you can just `not` it.

> True is not the same as 1

For historical reasons, in Python it is:

>>> bool.mro()
[<class 'bool'>, <class 'int'>, <class 'object'>]
>>> True == 1
True
>>> False == 0
True

So when you call ~True, you're calling `int.__invert__(True)`, which behaves as what it is: the bitwise inverse of a two's-complement signed integer.
History
Date User Action Args
2022-04-11 14:59:50adminsetgithub: 89531
2021-10-05 08:43:40serhiy.storchakasetstatus: open -> closed
resolution: not a bug
stage: resolved
2021-10-05 06:16:57xmorelsetnosy: + xmorel
messages: + msg403196
2021-10-05 04:52:40nmadurkarsetmessages: + msg403192
2021-10-05 04:40:36eric.smithsetnosy: + eric.smith
messages: + msg403191
2021-10-05 04:26:55nmadurkarcreate