|
发表于 2009-3-24 10:33:37
|
显示全部楼层
来个位域的用法。
#include "Stdio.h"
#include "Conio.h"
typedef union
{
struct
{
unsigned one:1;
unsigned two:2;
unsigned three:3;
}BITS;
unsigned char l;
}MY_TYPE;
int main(void)
{
/* 此处添加你自己的代码 */
MY_TYPE val;
MY_TYPE *ptr;
val.BITS.one=1;
val.BITS.two=2;
val.BITS.three=3;
printf("%x\n",val.BITS.one);
printf("%x\n",val.BITS.two);
printf("%x\n",val.BITS.three);
ptr=&val;
ptr->l=0x12abcdef;
printf("-------------------\n");
printf("%x\n",val.BITS.one);
printf("%x\n",val.BITS.two);
printf("%x\n",val.BITS.three);
printf("-------------------\n");
printf("%x\n",ptr->BITS.one);
printf("%x\n",ptr->BITS.two);
printf("%x\n",ptr->BITS.three);
printf("sizeof(val)=%d\n",sizeof(val));
getch();
return 0;
} |
|