Each Answer to this Q is separated by one/two green lines.
In Python console:
Why? Can someone explain this particular case to me in binary?
-2 in Two’s complement1
1 Flip all the bits, add 1 to the resulting number and interpret the result as a binary representation of the magnitude and add a negative sign (since the number begins with 1):
11111110 ? 00000001 ? 00000010 ? ? Flip Add 1
Which is 2, but the sign is negative since the MSB is 1.
bool, you’ll find that it’s numeric in nature – It has two values,
False, and they are just “customized” versions of the integers 1 and 0 that only print themselves differently. They are subclasses of the integer type
So they behave exactly as 1 and 0, except that
repr to display them differently.
>>> type(True) <class 'bool'> >>> isinstance(True, int) True >>> True == 1 True >>> True is 1 # they're still different objects False
bool type is a subclass of
int (for historical reasons; booleans were only added in Python 2.3).
See PEP 285 for why
bool is a subclass of
If you wanted the boolean inverse, use
>>> not True False >>> not False True
If you wanted to know why
-2, it’s because you are inverting all bits in a signed integer;
1111110 which in a signed integer is a negative number, see Two’s complement:
>>> # Python 3 ... >>> import struct >>> format(struct.pack('b', 1), '08b') '00000001' >>> format(struct.pack('b', ~1), '08b') '11111110'
where the initial
1 bit means the value is negative, and the rest of the bits encode the inverse of the positive number minus one.
~True == -2 is not surprising if
~ means bitwise inversion…
Truecan be treated as an integer and
- integers are represented in Two’s complement
- fixed the mixing between integer representation and bitwise inversion operator
- applied another polishing (the shorter the message, the more work needed)