union {
struct {
unsigned b0:1;
unsigned b1:1;
unsigned b2:1;
unsigned b3:1;
unsigned b4:1;
unsigned b5:1;
unsigned b6:1;
unsigned b7:1;
}oneBit;
unsigned char allBits;
} myFlag;
#define fast myFlag.oneBit.b0
#define mid myFlag.oneBit.b1
#define slow myFlag.oneBit.b2
#define flow myFlag.oneBit.b3
#define voice myFlag.oneBit.b4
#define pause myFlag.oneBit.b5
#define white myFlag.oneBit.b6
#define on_off myFlag.oneBit.b7
#define mark myFlag.allBits
上面的语句在PICC中编译出来每一个占一个位,但在KEIL C中编译出来怎么占一个字节啊?如果在在KEIL C 中要实现如上功能,应该怎么写呢?
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
单个位定义
bit flag;
bit b;
if(flag)....
b = flag;
组合方式
char bdata my_flag;
sbit flag0 = my_flag^0;
sbit flag1 = my_flag^1;
sbit flag2 = my_flag^2;
...
my_flag = 3;
if(flag0){
...
}
typedef struct
{
unsigned b0:1;
unsigned b1:1;
unsigned b2:1;
unsigned b3:1;
unsigned b4:1;
unsigned b5:1;
unsigned b6:1;
unsigned b7:1;
}oneBitStructs;
typedef union
{
oneBitStructs oneBit;
// unsigned char allBits;
}myFlag;
myFlag flag;
void main(void)
{
int size = sizeof(myFlag);
size = sizeof(oneBitStructs);
while(1)
;
}
优化级别设为0
单步调试过程可以看到myFlag这个联合体占用了2字节,结构体oneBitStructs也是一样
一周热门 更多>