在c语言中char类型的取值范围是(-128-127).
1. -128的补码到底是多少?
通过将下面的对应关系写出来,就可以发现。-128的补码其实就是1000 0000。
原码 补码 值
0111 1111 0111 1111 +127
0111 1110 0111 1110 +126
... .. 补码不断-1...
0000 0000 0000 0000 0
1000 0001 1111 1111 -1
1000 0010 1111 1110 -2
1000 0011 1111 1101 -3
... .. 补码不断-1...
1111 1111 1000 0001 -127
无法表达 1000 0000 -128
以上图表来源于http://www.cnblogs.com/flowerslip/p/5933833.html,如果原作者对我的引用有任何问题,可以与我沟通。
少废话,看代码
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4
5 void main(void)
6 {
7 char a = -128;
8 printf("The origin number is %x\n", a);
9 }
运行结果:The origin number is ffffff80
可以看出其补码就是1000 0000。
对原码和补码的掌握是计算机的基石,可能很多人早就熟悉这些,我再写出来也只是想提醒自己不要好高骛远,要脚踏实地。