[转]位域具体存放数值测试

与位域相关的测试代码

 

位域具体存放数值测试
#include <iostream>
#include
<windows.h>
using namespace std;
void main()
{
struct test
{
//在这里测试就可以发现其规律,有无unsigned、a:1 a:2 a:4 a:8 a:16 a:32 以及用char的各值来试验就可发现位域的规律
unsigned int a:8;
};
test t1;
t1.a
= 0;
cout
<<"十进制 -> 0: "<<dec<<t1.a<<endl;
cout
<<"十六进制-> 0: "<<hex<<t1.a<<endl;
cout
<<endl;
t1.a
= 1;
cout
<<"十进制 -> 1: "<<dec<<t1.a<<endl;
cout
<<"十六进制-> 1: "<<hex<<t1.a<<endl;
cout
<<endl;
t1.a
= 2;
cout
<<"十进制 -> 2: "<<dec<<t1.a<<endl;
cout
<<"十六进制-> 2: "<<hex<<t1.a<<endl;
cout
<<endl;
t1.a
= 3;
cout
<<"十进制 -> 3: "<<dec<<t1.a<<endl;
cout
<<"十六进制-> 3: "<<hex<<t1.a<<endl;
cout
<<endl;
t1.a
= 126;
cout
<<"十进制 -> 126: "<<dec<<t1.a<<endl;
cout
<<"十六进制-> 126: "<<hex<<t1.a<<endl;
cout
<<endl;
t1.a
= 127;
cout
<<"十进制 -> 127: "<<dec<<t1.a<<endl;
cout
<<"十六进制-> 127: "<<hex<<t1.a<<endl;
cout
<<endl;
t1.a
= 128;
cout
<<"十进制 -> 128: "<<dec<<t1.a<<endl;
cout
<<"十六进制-> 128: "<<hex<<t1.a<<endl;
cout
<<endl;
t1.a
= 129;
cout
<<"十进制 -> 129: "<<dec<<t1.a<<endl;
cout
<<"十六进制-> 129: "<<hex<<t1.a<<endl;
cout
<<endl;
t1.a
= 253;
cout
<<"十进制 -> 253: "<<dec<<t1.a<<endl;
cout
<<"十六进制-> 253: "<<hex<<t1.a<<endl;
cout
<<endl;
t1.a
= 254;
cout
<<"十进制 -> 254: "<<dec<<t1.a<<endl;
cout
<<"十六进制-> 254: "<<hex<<t1.a<<endl;
cout
<<endl;
t1.a
= 255;
cout
<<"十进制 -> 255: "<<dec<<t1.a<<endl;
cout
<<"十六进制-> 255: "<<hex<<t1.a<<endl;
cout
<<endl;
t1.a
= 256;
cout
<<"十进制 -> 256: "<<dec<<t1.a<<endl;
cout
<<"十六进制-> 256: "<<hex<<t1.a<<endl;
cout
<<endl;
t1.a
= 257;
cout
<<"十进制 -> 257: "<<dec<<t1.a<<endl;
cout
<<"十六进制-> 257: "<<hex<<t1.a<<endl;
cout
<<endl;
t1.a
= 258;
cout
<<"十进制 -> 258: "<<dec<<t1.a<<endl;
cout
<<"十六进制-> 258: "<<hex<<t1.a<<endl;
cout
<<endl;
t1.a
= 259;
cout
<<"十进制 -> 259: "<<dec<<t1.a<<endl;
cout
<<"十六进制-> 259: "<<hex<<t1.a<<endl;
cout
<<endl;
t1.a
= 260;
cout
<<"十进制 -> 260: "<<dec<<t1.a<<endl;
cout
<<"十六进制-> 260: "<<hex<<t1.a<<endl;
cout
<<endl;
t1.a
= 261;
cout
<<"十进制 -> 261: "<<dec<<t1.a<<endl;
cout
<<"十六进制-> 261: "<<hex<<t1.a<<endl;
cout
<<endl;
t1.a
= 262;
cout
<<"十进制 -> 262: "<<dec<<t1.a<<endl;
cout
<<"十六进制-> 262: "<<hex<<t1.a<<endl;
cout
<<endl;
t1.a
= 263;
cout
<<"十进制 -> 263: "<<dec<<t1.a<<endl;
cout
<<"十六进制-> 263: "<<hex<<t1.a<<endl;
cout
<<endl;
t1.a
= 264;
cout
<<"十进制 -> 264: "<<dec<<t1.a<<endl;
cout
<<"十六进制-> 264: "<<hex<<t1.a<<endl;
cout
<<endl;
t1.a
= 65533;
cout
<<"十进制 -> 65533: "<<dec<<t1.a<<endl;
cout
<<"十六进制-> 65533: "<<hex<<t1.a<<endl;
cout
<<endl;
t1.a
= 65534;
cout
<<"十进制 -> 65534: "<<dec<<t1.a<<endl;
cout
<<"十六进制-> 65534: "<<hex<<t1.a<<endl;
cout
<<endl;
t1.a
= 65535;
cout
<<"十进制 -> 65535: "<<dec<<t1.a<<endl;
cout
<<"十六进制-> 65535: "<<hex<<t1.a<<endl;
cout
<<endl;
t1.a
= 65536;
cout
<<"十进制 -> 65536: "<<dec<<t1.a<<endl;
cout
<<"十六进制-> 65536: "<<hex<<t1.a<<endl;
cout
<<endl;
t1.a
= 65537;
cout
<<"十进制 -> 65537: "<<dec<<t1.a<<endl;
cout
<<"十六进制-> 65537: "<<hex<<t1.a<<endl;
cout
<<endl;
}

 

posted @ 2010-08-15 22:21  百分百好牛  阅读(236)  评论(0编辑  收藏  举报