Complement code, inverse code, original code and python operators~

**

In python, ~1=-2, ~60 = -61, why is this?

**
First of all, the ~ operator is a bitwise inversion operation in Python. The binary number of 1 is 0000 0001, and the bitwise inversion becomes 1111 1110, the binary digit The highest bit in is the sign bit. After inverting 1, it becomes a negative number. Okay, let’s introduce the complement code, complement code, and original code.
The original code and complement code of positive integer numbers in computers , the complement code is the same, but there is a difference with negative integers. The complement code is the bit-by-bit inversion of all the bits of the original code except the highest bit. Add 1 to the complement code to get the complement code. Integers in computers are stored in two's complement form, so the above 1111 1110 is in two's complement form. In order to intuitively express how much 1 is after inverting it, we convert this complement code into the original code and it will be clear. First of all Subtract 1 from 1111 1110 and change it back to the complement code 1111 1101, and then remove the highest bit and invert it to the original code 1000 0010, which is -2, so the reason is this.
Learning, an hour of pain, a whole day of happiness

Guess you like

Origin blog.csdn.net/qq_41841073/article/details/117771046#comments_29672900