#include <stdlib.h>
#include <stdio.h>
#include <string.h>
typedef struct
{
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;
unsigned char b8 :1;
} BIT_S;
int main()
{
BIT_S bit1;
memset(&bit1, 0, sizeof(BIT_S));
bit1.b1 = 1;
bit1.b4 = 1;
bit1.b7 = 1;
printf("b1:%d, b2:%d, b4:%d,b5:%d b7:%d b8:%d",
bit1.b1, bit1.b2, bit1.b4, bit1.b5, bit1.b7, bit1.b8);
return 0;
}
输出结果:
b1:1, b2:0, b4:1,b5:0 b7:1 b8:0
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
typedef struct
{
char b1 :1;
char b2 :1;
char b3 :1;
char b4 :1;
char b5 :1;
char b6 :1;
char b7 :1;
char b8 :1;
} BIT_S;
int main()
{
BIT_S bit1;
memset(&bit1, 0, sizeof(BIT_S));
bit1.b1 = 1;
bit1.b4 = 1;
bit1.b7 = 1;
printf("b1:%d, b2:%d, b4:%d,b5:%d b7:%d b8:%d",
bit1.b1, bit1.b2, bit1.b4, bit1.b5, bit1.b7, bit1.b8);
return 0;
}
输出结果:
b1:-1, b2:0, b4:-1,b5:0 b7:-1 b8:0
以上两部分代码差别 char 与 unsigned char