#include <stdio.h> int main(void) { //-1为整数,内存中存储为四个字节 //-1原码:10000000000000000000000000000001 //-1反码:11111111111111111111111111111110 //-1补码:11111111111111111111111111111111 char a = -1;//整型-1赋给char a 时进行截取操作,此时a存储为1 1111111 signed char b = -1;//整型-1赋给signed char b 时进行截取操作,此时b存储为1 1111111 //终上所述,char默认为signed char unsigned char c = -1;//整型-1赋给 unsigned char c 时进行截取操作,此时c存储为1 1111111 //输出结果为:a=-1,b=-1,c=255 //输出格式为%d,即整型形式,所以由char到int需要实现整型提升 //提升过程:无符号通过添加0进行提升,有符号通过添加1提升。 //即有符号的a,b提升后结果一样为:由1 1111111 提升为 11111111111111111111111111111111(补码形式)==》转化为原码:10000000000000000000000000000001 = -1 //即无符号的c提升后结果一样为:由1 1111111 提升为00000000000000000000000011111111(无符号或者说正数的原码反码补码都一致)=255 printf("a=%d,b=%d,c=%d",a,b,c); return 0; }
注:1.截断规则
例子:1000 1101截断成四位===》 1101(舍弃前面多余的)
2.计算机中运算是通过补码进行运算,但是结果是转换为原码输出。