#include <iostream>
#include <string.h>
using namespace std;
int main ()
{
char a =-1;
unsigned char b = 255;
printf ("a = 0x%x b = 0x%x \ n", A, b);
System ("pause");
return 0;
}
The results of the operation are as follows:
Analytical:
The reason that the two are not the same as the output is because%x is a 16-in-print unsigned int (unsigned int). Therefore, when printing, there are two implicit type conversions:
Note 1:
Because in the case of coding as complement, there are two types of elevation: 1. Symbolic extension: For a signed number, the method of extending the storage bit number. Fills the new High-order byte with the value of the current most significant bit, the sign bit. (sign bits are all in 0 for "positive" and 1 for "negative" 2.) 0 extension: For unsigned numbers, the method of extending the storage bit number. Fill in the new high 0 directly. NOTE 2:-5 corresponds to positive 5 (original code: 00000101) → all bit counter (reverse code: 11111010) → plus 1 (complement: 11111011)
1, Char => unsigned int:a is stored in the computer system as 0xFF, because it is a symbolic extension, so the number of unsigned integers is: 0xFFFFFFFF.
2, unsigned char => unsigned int:b stored in the computer system as 0xFF, because it is 0 expansion, so the number of unsigned integers is still: 0xFF.
Reference blog:
Type promotion in C--basic concept, but there's still a lot of people who can't figure out