short、int、long类型有符号位输出和无符号位输出的数值问题
原码、反码、补码转换
系统:windows 32/windows 64
软件:Visual C++ 6.0
#include<stdio.h>
void main()
{
printf(" 类型 = 字节数\n");
printf(" sizeof(short) = %d\n",sizeof(short));
printf("sizeof(unsigned short) = %d\n",sizeof(unsigned short));
printf(" sizeof(int) = %d\n",sizeof(int));
printf(" sizeof(unsigned int) = %d\n",sizeof(unsigned int));
printf(" sizeof(long) = %d\n",sizeof(long));
printf(" sizeof(unsigned long) = %d\n",sizeof(unsigned long));
}
运行结果:
long类型为四个字节,取值范围为-2^31~2^31-1,即-2147483648~2147483647
#include<stdio.h>
void main()
{
long i = 2147483648;
long _i = -2147483648;
long maxi = 2147483649;
long _maxi = -2147483649;
long mini = 2147483647;
long _mini = -2147483647;
printf(" 数值 = 无符号八进制 有符号十进制 无符号十进制 无符号十六进制\n");
printf(" 2147483647 = %o\t %d\t %u\t %x\n",mini,mini,mini,mini);
printf("-2147483647 = %o\t %d\t %u\t %x\n",_mini,_mini,_mini,_mini);
printf(" 2147483648 = %o\t %d\t %u\t %x\n",i,i,i,i);
printf("-2147483648 = %o\t %d\t %u\t %x\n",_i,_i,_i,_i);
printf(" 2147483649 = %o\t %d\t %u\t %x\n",maxi,maxi,maxi,maxi);
printf(