Bitwise NOT is the first complement, for example:
x = 1 (binary: 0001)~x = -2 (binary: 1110) Hence, my question is why -2 in binary is (-0b10) as for the python compiler?
We know that 1110 represents (14) for unsigned integer and (-2) for signed integer.
Two's complement inherently depends on the size of a number. For example, -2 on signed 4-bit is 1110 but on signed 8-bit is 1111 1110.
Python's integer type is arbitrary precision. That means there is no well-defined leading bit to indicate negative sign or well-defined length of the two's complement. A two's complement would be 1... 1110, where ... is an infinite repetition of 1.
As such, Python's integer are displayed as a separate sign (nothing or -) and the absolute number. Thus, -2 becomes - and 0b10 – i.e. - 2. Similarly, -5 becomes - and 0b101 – i.e. - 5.
Note that this representation is merely the standard representation to be human-readable. It is not necessarily the internal representation, which is implementation defined.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With