-char num = 0;
+unsigned char num = 0;
或者
-char num = 0;
+unsigned char num = 0;
char,unsigned char和signed char是不同的。
char被译成unsigned还是signed,依赖于具体实现,c/c++本身并没有规定。
如果只关心文本形式而不关心数值,应该用char。
但如果依赖于数值,应该根据需要使用unsigned char或signed char。
而要能完整表示所有的8比特二进制,数值范围是0~255,只能用unsigned char,当然如果是int就更没问题了。
D3的2进制为11010011,如果是signed char,最高位表示符号,根据补码其值为-45,当promote为signed int时,按照Sign extension进行保号操作,便成了FFFFFFD3:
Sign extension is the operation, in
computer arithmetic, of increasing the
number of bits of a binary number
while preserving the number's sign
(positive/negative) and value. This is
done by appending digits to the most
significant side of the number,
following a procedure dependent on the
particular signed number
representation used.