|
发表于 2020-4-7 09:04:05
|
显示全部楼层
本帖最后由 ssffzz1 于 2020-4-7 09:19 编辑
抱歉我看错了,还是要加那个struct a的。但这样访问比较麻烦。我提供了另外一种方法:
#include <stdio.h>
#include <stdlib.h>
typedef union{
unsigned char v;
struct{
unsigned char b0:1;
unsigned char b1:1;
unsigned char b2:1;
unsigned char b3:1;
unsigned char b4:1;
unsigned char b5:1;
unsigned char b6:1;
unsigned char b7:1;
}a;
#define b0 a.b0
#define b1 a.b1
#define b2 a.b2
#define b3 a.b3
#define b4 a.b4
#define b5 a.b5
#define b6 a.b6
#define b7 a.b7
}BITCHAR;
int
main(void)
{
BITCHAR x;
x.v=0x53;
printf("xv=%x\n",x.v);
printf("b0=%d,b1=%d,b2=%d,b3=%d\n",x.b0,x.b1,x.b2,x.b3);
printf("b4=%d,b5=%d,b6=%d,b7=%d\n",x.b4,x.b5,x.b6,x.b7);
return(EXIT_SUCCESS);
}
|
|