c++如何定义位域??????
#include "stdafx.h"
#include <iostream>
using namespace std;
union k{
int a:1
int :2 /*该2位不能使用*/
int b:3
int c:2
};
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}
[解决办法]
struct flags
{
unsigned a:1;
unsigned :2; /*该2位不能使用*/
unsigned b:3;
unsigned c:2;
unsigned d:4;
unsigned :0 ;//下个位段从下个字节算起。
unsigned e:5;
};
int main(){
flags f={1,2,3,6,7};
printf("%u,%u,%u,%u,%u",f.a,f.b,f.c,f.d,f.e);
union {
flags uf;
int ux;
};
//函数内部的无名公共体,变量名可以独立使用,所有变量占用同一空间。
uf = f;
printf("ux = %08x\n",ux);
return 0;
}