目的
目前有十六个数需要存储状态,1 | 0;
16长度,先想到的是两个字节,short 类型;
举例
界面存储的是32个复选框,背景色是绿色,点击选择后是红色
根据选择的复选框,存储状态,
两个思路:
1 数据结构
typedef union _SWITCHS
{
struct _S
{
unsigned int switch_1 : 1 ;
unsigned int switch_2 : 1 ;
unsigned int switch_3 : 1 ;
unsigned int switch_4 : 1 ;
unsigned int switch_5 : 1 ;
unsigned int switch_6 : 1 ;
unsigned int switch_7 : 1 ;
unsigned int switch_8 : 1 ;
unsigned int switch_9 : 1 ;
unsigned int switch_10 : 1 ;
unsigned int switch_11 : 1 ;
unsigned int switch_12 : 1 ;
unsigned int switch_13 : 1 ;
unsigned int switch_14 : 1 ;
unsigned int switch_15 : 1 ;
unsigned int switch_16 : 1 ;
}S;
unsigned char c[2];
unsigned short int i;
}SWITCHS;
switch1.i = 0; //初始化赋值为0
if(ui->checkBox_one_1->isChecked())
{
switch1.S.switch_1 = 1;
}
else
{
switch1.S.switch_1 = 0;
}
.........//一次取值 23456...
2 或| 运算符
unsigned short int group1;
unsigned short int bitTab[16] = {0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,0X100,0X200,0X400,0X800,0X1000,0X2000,0X4000,0X8000};
group1 = 0; //初始化赋值为0
if(ui->checkBox_group_1->isChecked())
{
group1 |= bitTab[0];
}
................//依次取值 23456....
以上两种方法都是可以的