-3

Why "~False" is -1 in Python?

I was using a boolean variable in Python. When I try to do not of that, it returns -1. I want to understand why so? why a variable a changing its data-type duo to this operation.

Trying to add more details

b0 = False
print(type(b0))
b0 = ~b0
print(type(b0))

>>bool
>>int
  • 1
    Can you give an example of the code you're running? – Hofbr Sep 24 '20 at 11:16
  • 3
    @B.Hoffman What more than `~False` do you need? – superb rain Sep 24 '20 at 11:16
  • 3
    Do you understand why ``~0`` is ``-1``? – MisterMiyagi Sep 24 '20 at 11:17
  • I do understand ~0 is -1 but ~False should be True. right? – Sourodeep Basu Sep 24 '20 at 11:19
  • 1
    @SourodeepBasu Well at least it's true. The bigger problem is that `~True` is also true :-) – superb rain Sep 24 '20 at 11:21
  • does this help? https://stackoverflow.com/questions/8305199/the-tilde-operator-in-python – yedpodtrzitko Sep 24 '20 at 11:21
  • 1
    Well False is equal to 0, so if you understand `~0`, that covers `~False`. – khelwood Sep 24 '20 at 11:22
  • Without more context, all I can tell you is that `True` and `False` in Python are represented as `1` and `0` respectively. `~` is the bitwise complement operator in python which essentially calculates `-x - 1` so since False is equivalent to 0 the bitwise complement of `False` is `-1` – Hofbr Sep 24 '20 at 11:24
  • 3
    I agree that `~False` being `-1` is surprising. It *should* arguably be a TypeError… – deceze Sep 24 '20 at 11:24
  • `bool` is a subclass of `int` ([see here](https://docs.python.org/3.3/library/functions.html#bool)) and the values for `True` & `False` are 0 & 1 resp. – Tibebes. M Sep 24 '20 at 11:24
  • @khelwood With that explanation, I'd also expect `str(False)` to be the same as `str(0)`. – superb rain Sep 24 '20 at 11:25
  • 1
    @superb Subclasses can override the `__str__`/`__repr__` implementation… – deceze Sep 24 '20 at 11:26
  • 3
    @superbrain It would be if bool didn't have its own repr implementation. bool does not have its own ~ implementation. Should we interpret the question as "why doesn't bool have its own ~ implementation?" – khelwood Sep 24 '20 at 11:28
  • 1
    @khelwood Just saying that "Equal inputs lead to equal outputs" (which is what that looked like) was lacking something. – superb rain Sep 24 '20 at 11:31
  • How much clarification does this topic still need? You did notice the duplicate this is closed as? Is something still unclear now that we have explained that bool is a subclass of int? – deceze Sep 24 '20 at 11:35

1 Answers1

1

The tilde ~ is the bitwise 'not' operator, not the boolean 'not' operator. To do a not you probably want 'not False'.

The reason for it changing its data type is that it treats False as binary 0 and then flips it to -1.

Carlos Gonzalez
  • 858
  • 13
  • 23
  • I am aware of that, I am wondering why python do that, why it changes the bool to an Int. – Sourodeep Basu Sep 24 '20 at 11:24
  • 1
    @SourodeepBasu it is not *changing*, try running `isinstance(False, int)` and see what happens – Tomerikoo Sep 24 '20 at 11:27
  • 1
    @Sourodeep As noted ... because it’s a **bitwise not**, not a **logical not**. – donkopotamus Sep 24 '20 at 11:27
  • The bool class is a subclass of int. The bitwise operator only works on ints thus False is treated as such. See https://docs.python.org/3/library/functions.html#bool. False itself it still a bool which technically is still an int in Python – Carlos Gonzalez Sep 24 '20 at 11:28
  • @Tomerikoo Arguably `~False` does change the class from `bool` to `int`. `bool` just happens to also be a valid `int`… – deceze Sep 24 '20 at 11:28
  • 1
    @deceze but what do you mean by *changes*? The class of the object doesn't magically change. It just inherits some methods from the parent `int` which in this case are not overriden – Tomerikoo Sep 24 '20 at 11:31
  • 1
    @Tomerikoo Well, `type(False) == bool`, `type(~False) == int`; that constitutes a class change for all appearances. (Sure, arguably an operation using any operators doesn't need to preserve the type of the operand, but you get the idea and why it looks to OP like a change of class.) – deceze Sep 24 '20 at 11:33
  • @deceze yeah ok now I get what you were trying to say. I was reading it as changing the source, rather than changing the return type... – Tomerikoo Sep 24 '20 at 11:37