Shouldn't the bitwise operator ~ only return binary?
Well, technically EVERYTHING in a computer is binary. However, this is only part of the story. There are two important concepts here:
- Data types of values
- Representations of those values
For the first one True is a boolean and -2 is an integer. In some cases, Python will convert a value from one type to another in order to perform certain operations. In this case, the ~ is the bitwise operator which only works on integers. In this case, an actual conversion isn't necessary since boolean inherits from int, so the boolean value can just be treated as the integer value of 1. Then ~ gives the bitwise inverse of that integer value.
Now if we represent the 1 in binary as 00000001 (yes, technically there are more 0s, but I'm not going to type them out...the concept still holds if we only use 8 bits instead of the actual 32 or 64), we negate this to 11111110 (again, there are more leading 1s because there are really more bits). Note that the way I write the integer value here in binary is merely a representation of the value using characters I can type on a keyboard. This is still 00000001 in binary and 1 in decimal both represent the same underlying value in memory. Similarly 11111110 binary and -2 decimal both represent the same value.
At the end, print() will just print the value in decimal which is why you get -2. If you want to print the value in hex or binary, there are built in methods to get these representations instead.