双字 (32个位) 4294967296
单字 (16个位) 65535
字节 (8 个位) 255
十六进制数 (4 个位) 15(F)
举例如下:
| 序号 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 0 | 1 | 2 | 3 | 4 | 5 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 数值 | 1 | 0 | 0 | 0 | 1 | 1 | 1 | 1 | 1 | 0 | 0 | 0 | 0 | 0 | 1 | 0 |
0xF1, 0x41
用十六进制表示为:
第一个字节:0xF1("1"表示第一个字节低4位,“F”表示第一个字节高4位)
第二个字节:0x41("1"表示第二个字节低4位,“4”表示第二个字节高4位)
short是16位,默认是有符号,所以范围是-32767~32767。下面用一段代码进行说明
#include
using namespace std;
int main()
{
int i = 1;
short s;
for(;i < 65537; i++)
{
cout << "i: " << i << endl;
s = short(i);
cout << "s: " << s << endl;
}
}
然后进行编译运行即可
#g++ int_short.cpp //此时会产生a.out可执行文件
#a.out 1>int_short.txt //将标准输出打印到文件中
输出结果如下
| 序号 | 0 | 1 | 2 | … | 32766 | 32767 | 32868 | 32869 | 32810 | … | 65534 | 65535 | 65536 | 65537 | 65538 | 65539 | … |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 数值 | 1 | 0 | 0 | … | 32766 | 32767 | -32868 | -32767 | 32766 | … | -2 | -1 | 0 | 1 | 2 | 3 | … |
这个规律自己去总结吧,我还不能从二进制的角度解释这种规律(大佬可以指点下)。
都是4个字节(32位)
uint无符号范围更大,int有符号。
uint:取值范围是232 - 1,即:0~4294967295。
int:数据范围为-2147483648 ~ 2147483647[-231~ 231-1]。
作用:
代码如下
#include
using namespace std;
int main()
{
int i = 5;
int a, b, c, d;
a = i << 1;
b = i << 2;
c = i >> 1;
d = i >> 2;
cout << "i: " << i << endl;
cout << "a: " << a << endl;
cout << "b: " << b << endl;
cout << "c: " << c << endl;
cout << "d: " << d << endl;
}
编译后输出如下
i: 5 // 00101
a: 10 // 01010
b: 20 // 10100
c: 2 // 00010
d: 1 // 00001
太直接了,一点儿都不费脑子。