首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

c++怎么定义位域?

2013-10-14 
c++如何定义位域??????#include stdafx.h#include iostreamusing namespace stdunion k{  int a:1  i

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;
}

热点排行