/********************************************************************
purpose: 位域测试
author: dotlive
*********************************************************************/
#include <iostream>
using namespace std;
union
{
struct
{
char i:1;
char j:2;
char m:3;
}s;
char ch;
}r;
struct
{
char i:1;
char j:2;
char m:3;
}node;
int main()
{
r.s.i = 1;
r.s.j = 2;
r.s.m = 3;
// |m |j |i
// |11 |10 |1
node.i = 'm';
node.j = 3.1415926;
node.m = 100;
cout<<"r.ch = "<<(int)r.ch<<" = 0x"<<hex<<(int)r.ch<<endl
<<"sizeof(r) = "<<sizeof(r)<<endl;
cout.unsetf(ios::hex);
cout<<"sizeof(node) = "<<sizeof(node)<<endl;
return 0;
}
purpose: 位域测试
author: dotlive
*********************************************************************/
#include <iostream>
using namespace std;
union
{
struct
{
char i:1;
char j:2;
char m:3;
}s;
char ch;
}r;
struct
{
char i:1;
char j:2;
char m:3;
}node;
int main()
{
r.s.i = 1;
r.s.j = 2;
r.s.m = 3;
// |m |j |i
// |11 |10 |1
node.i = 'm';
node.j = 3.1415926;
node.m = 100;
cout<<"r.ch = "<<(int)r.ch<<" = 0x"<<hex<<(int)r.ch<<endl
<<"sizeof(r) = "<<sizeof(r)<<endl;
cout.unsetf(ios::hex);
cout<<"sizeof(node) = "<<sizeof(node)<<endl;
return 0;
}
![位域测试 - dotlive - DotLive的博客](https://i-blog.csdnimg.cn/blog_migrate/cafbf3447645a2836bed7fc0728ca590.png)