一.整数的表示
由于计算机只能识别二进制的0和1,在存储数据时就涉及到了对数据的编码问题,计算机中对整数的编码有两种不同的方式,分别为:
1. 只能表示非负数,此种类型数据称为无符号类型 即 unsigned
2. 能表示负数0和正数,此种类型数据称为有符号类型 signed
二.整型数据类型的表示范围
C数据类型 |
字节数 |
编码方式 |
能表示的最小值 |
能表示的最大值 |
unsigned char |
1 |
原码 |
0 (0x00) |
128 (0xFF) |
char |
1 |
补码 |
-128 (0x80 ) |
127 (0x7F) |
unsigned short |
2 |
原码 |
0 (0x00 00) |
65535(0xFFFF) |
short |
2 |
补码 |
-32768(0x8000) |
32767(0x7FFF) |
unsigned int |
4 |
原码 |
(0x0000 0000) |
(0xFFFF FFFF) |
int |
4 |
补码 |
(0x8000 0000) |
(0x7FFF FFFF) |
unsigned long |
4 |
原码 |
(0x0000 0000) |
(0xFFFF FFFF) |
long |
4 |
补码 |
(0x8000 0000) |
(0x7FFF FFFF) |
unsigned long long |
8 |
原码 |
(0x0000 0000 0000 0000) |
(0xFFFF FFFF FFFF ) |
long long |
8 |
补码 |
(0x8000 0000 0000 0000 ) |
(0x7FFF FFFF FFFF FFFF) |
32位环境的验证:
sizeof(char) = 1byte
sizeof(unsigned char) = 1byte
sizeof(short int) = 2byte
sizeof(unsigned short int) = 2byte
sizeof(int) = 4byte
sizeof(unsigned int) = 4byte
sizeof(long) = 4byte
sizeof(unsigned long) = 4byte
sizeof(long long) = 8byte
sizeof(unsigned long long) = 8byte
64位环境整型数据的取值范围:
C数据类型 |
字节数 |
编码方式 |
能表示的最小值 |
能表示的最大值 |
unsigned char |
1 |
原码 |
0 (0x00) |
128 (0xFF) |
char |
1 |
补码 |
-128 (0x80 ) |
127 (0x7F) |
unsigned short |
2 |
原码 |
0 (0x00 00) |
65535(0xFFFF) |
short |
2 |
补码 |
-32768(0x8000) |
32767(0x7FFF) |
unsigned int |
4 |
原码 |
(0x0000 0000) |
(0xFFFF FFFF) |
int |
4 |
补码 |
(0x8000 0000) |
(0x7FFF FFFF) |
unsigned long |
8 |
原码 |
(0x0000 0000 0000 0000) |
(0xFFFF FFFF FFFF ) |
long |
8 |
补码 |
(0x8000 0000 0000 0000 ) |
(0x7FFF FFFF FFFF FFFF) |
unsigned long long |
8 |
原码 |
(0x0000 0000 0000 0000) |
(0xFFFF FFFF FFFF ) |
long long |
8 |
补码 |
(0x8000 0000 0000 0000 ) |
(0x7FFF FFFF FFFF FFFF) |
64位环境的验证;
sizeof(char) = 1
sizeof(unsigned char) = 1
sizeof(short int) = 2
sizeof(unsigned short int) = 2
sizeof(int) = 4
sizeof(unsigned int) = 4
sizeof(long) = 8
sizeof(unsigned long) = 8
sizeof(long long) = 8
sizeof(unsigned long long) = 8
三.无符号数的编码