The original code, the inverted, and bit complement arithmetic

reference:

https://www.zhihu.com/question/20159860

https://www.cnblogs.com/zhangziqiu/archive/2011/03/30/ComputerCode.html

 

Summarize some of the points

In order to make the computer easier operation data, converted into an addition subtraction: 1 - 1 => 1 + (-1). To support this function, the computer storage of data requires some additional processing.

 

Original code: the most significant bit as the sign bit, i.e. for the difference between -1 and 1, only the most significant bit is not the same.

Anti-Code: positive anti-code and the original code is the same, but negative, except the most significant bit is 1, the remaining bits inverted.

Complement: positive number and complement the original code is the same, but negative complement is negative inverse code + 1 + may trigger a carry after 1, resulting in the highest level changes from 1 to 0, all such cases the bits are 0, i.e. the digital value of 0, 0 because there is no positive and negative points, it can be considered complement notation, the most significant bit is a computer that are negative, the rest are 0 or a positive number

 

Computer using the encoding mode to store digital complement , as represented in figures 4 to length 3 + (-3):

3 the complement of the original code = 0011 = 3

-3 = -3 inverse complement code (1100) + 1 = 1101

0011 + 1101 = 10000, because there is a four digits, the excess is discarded, the calculation result is 0000 = 0

 

According to the law of complement, the actual value is calculated as 

After 1010 0000 0000 0000 0000 000000000000-1

1,001,111,111,111,111 1,111,111,111,111,111 again outside the exception of the highest bit inversion

1,110,000,000,000,000 0,000,000,000,000,000, except after the sign bit, the other is the actual position of positive values, and copied to the calculator:

Positive value corresponding to the number 1610612736, the complement value corresponding to the above -1610612736

 

Bit computing

value >> n: the complementary digital right by n bits, the most significant bit remains unchanged , the length of the digital 4: 1010 (-6) into a right 1101 (-3), 0111 (7) a right to 0011 (3)

value << n: n-bit digital complement to the left, the lowest level 0s, left during the MSB will change, positive and negative numbers may be reversed in order to demonstrate the above -1610612736 about this process:

Is the corresponding complement: 1,010,000,000,000,000 0000 0000 0000 0000, the left shift value after 0,100,000,000,000,000 0000 0000 0000 0000 corresponding to a:

 The program runs, the same result:

 It can not be simply understood as the left is multiplied by 2, otherwise the program susceptible to bug.

value >>> n: with the above right, one more> except that the highest level is not maintained, but rather complement fixed 0, which means, for negative numbers, the right 0s after it It turned into a positive.

Guess you like

Origin www.cnblogs.com/hellohello/p/11881258.html