图片附件: 游客没有浏览图片的权限,请
登录 或
注册
#include <stdio.h>
union ucb {
struct cb_bits {
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;
} cb_bits;
unsigned char cb_byte;
#define cb_bit0
cb_bits.b0
#define cb_bit1
cb_bits.b1
#define cb_bit2
cb_bits.b2
#define cb_bit3
cb_bits.b3
#define cb_bit4
cb_bits.b4
#define cb_bit5
cb_bits.b5
#define cb_bit6
cb_bits.b6
#define cb_bit7
cb_bits.b7
};
int main(void)
{
union ucb ucb;
ucb.cb_byte = 0x7c;
printf("%d", ucb.cb_bit7);
printf("%d", ucb.cb_bit6);
printf("%d", ucb.cb_bit5);
printf("%d", ucb.cb_bit4);
printf("%d", ucb.cb_bit3);
printf("%d", ucb.cb_bit2);
printf("%d", ucb.cb_bit1);
printf("%d", ucb.cb_bit0);
printf("\n");
printf("0x%02x\n", ucb.cb_byte);
ucb.cb_bit7 = 1;
printf("%d", ucb.cb_bit7);
printf("%d", ucb.cb_bit6);
printf("%d", ucb.cb_bit5);
printf("%d", ucb.cb_bit4);
printf("%d", ucb.cb_bit3);
printf("%d", ucb.cb_bit2);
printf("%d", ucb.cb_bit1);
printf("%d", ucb.cb_bit0);
printf("\n");
printf("0x%02x\n", ucb.cb_byte);
return 0;
}