Complement verification

Decimal represent print a hexadecimal number

#include <stdio.h> int main ( int argc, char const * the argv []) 
{ // . 1 byte number, hexadecimal, 2
     // 1000 0001 = 0x81
     // binary, octal, sixteen angle hex need a computer, which is considered to complement char a = 0x81 ; 
    the printf ( " a D =% \ n- " , a); // -127 return 0 ; 
}

 
    
    
     

Output 81 hexadecimal to decimal -127, for the following reasons:

Variables a = 0x81, which is the value of 81 of complement format, i.e. binary 1000 0001, and the output of the original code is -127, 1111 1111 binary representation, the intermediate process is as follows:

1, according to the original code twos complement (negative):

1) The highest bit sign, the other bit binary value (original code)

2) On the basis of 1), based on the sign bit unchanged, the other bit is inverted

3) the complement of 2) was added based on the 1

2, according to the original request code complement, twos complement, and the same procedure

1) twos complement

2) On the basis of 1), based on the sign bit unchanged, the other bit is inverted

3) the complement of 2) was added based on the 1

The above 0x81 twos complement to 10,000,001, the highest bit is 1, that is a negative number.

1111 1110 it is inverted, after adding 1 to the original code 1111 1111, sign bit is not included in the maximum value, the remaining 7 1, i.e. 127, -127 plus sign

Therefore, the final output is -127.

The key point is:

See decimal, from the user point of view, the issue of perspective to the original code

See binary, octal, hexadecimal, standing in the computer point of view, to think about the problem in order to complement angle

Guess you like

Origin www.cnblogs.com/jixiaohua/p/11073711.html