Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

~(True) and ~(False) gives incorrect result #89531

Closed
nmadurkar mannequin opened this issue Oct 5, 2021 · 4 comments
Closed

~(True) and ~(False) gives incorrect result #89531

nmadurkar mannequin opened this issue Oct 5, 2021 · 4 comments

Comments

@nmadurkar
Copy link
Mannequin

nmadurkar mannequin commented Oct 5, 2021

BPO 45368
Nosy @ericvsmith, @masklinn

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2021-10-05.08:43:40.795>
created_at = <Date 2021-10-05.04:26:55.477>
labels = ['invalid']
title = '~(True) and ~(False) gives incorrect result'
updated_at = <Date 2021-10-05.08:43:40.795>
user = 'https://bugs.python.org/nmadurkar'

bugs.python.org fields:

activity = <Date 2021-10-05.08:43:40.795>
actor = 'serhiy.storchaka'
assignee = 'none'
closed = True
closed_date = <Date 2021-10-05.08:43:40.795>
closer = 'serhiy.storchaka'
components = []
creation = <Date 2021-10-05.04:26:55.477>
creator = 'nmadurkar'
dependencies = []
files = []
hgrepos = []
issue_num = 45368
keywords = []
message_count = 4.0
messages = ['403190', '403191', '403192', '403196']
nosy_count = 3.0
nosy_names = ['eric.smith', 'xmorel', 'nmadurkar']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = 'resolved'
status = 'closed'
superseder = None
type = None
url = 'https://bugs.python.org/issue45368'
versions = []

@nmadurkar
Copy link
Mannequin Author

nmadurkar mannequin commented Oct 5, 2021

~(True) returns -2
~(False) returns -1

@ericvsmith
Copy link
Member

What were you expecting?

I think those are correct. See:
>>> ~1
-2
>>> ~0
-1

@nmadurkar
Copy link
Mannequin Author

nmadurkar mannequin commented Oct 5, 2021

True is a boolean so ~True should return False according to me.
True is not the same as 1

@masklinn
Copy link
Mannequin

masklinn mannequin commented Oct 5, 2021

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.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants