1. Machine number, truth value, original code, inverted code, complementary code
https://www.cnblogs.com/zhangziqiu/archive/2011/03/30/ComputerCode.html
Machine number : The binary representation of a number in a computer. The machine number is signed. The computer uses the most significant digit of a number to store the sign.
True value : Because the first bit is the sign bit, the formal value of the machine number is not equal to the true value. The true value corresponding to the machine number with the sign bit is called the true value of the machine number
Why do you need to set the original code, inverted code, and complementary code : to facilitate the sign bit to participate in the operation
Why does the computer store the complement code when storing the data : There is only an adder in the computer, and only the addition operation can be performed
2. How to get the complement of numbers in Python?
- bin function: return binary with negative sign instead of complement
>>> bin(10)
'0b1010'
>>> bin(-10)
'-0b1010'
- Perform AND operation on the value to obtain the complement form (8 bits: & 0b11111111, 16 bits: & 0b1111111111111111, ...)
>>> bin(10&0b11111111)
'0b1010'
>>> bin(-10&0b11111111)
'0b11110110'
- Universal format
# 获得8位补码
>>> bin(10&int('1'*8,2))
'0b1010'
>>> bin(-10&int('1'*8,2))
'0b11110110'
# 获得32位补码
>>> bin(-10&int('1'*32,2))
'0b11111111111111111111111111110110'
>>> bin(10&int('1'*32,2))
'0b1010'
3. Bit operation
http://www.361way.com/bitwise-operators/5778.html
pending upgrade