69,371
社区成员
发帖
与我相关
我的任务
分享
struct STR1 {
unsigned int a:3;
unsigned int b:2;
unsigned int c:3; //111 11 111都在一个字节里,#####置1为有效位#####
};
struct STR2 {
unsigned int b1:5; //0001 1111第一个字节
unsigned int b2:2; //0000 0011第二个字节
};
/* 1001 1001 0x99
* 输出
* 1 a 001
* -1 b 11
* -4 c 100
*
* -7 b1 11001
* 0 b2 00
*/
#include <stdio.h>
union share{
char val;
struct STR1 {
unsigned a:3;
unsigned b:2;
unsigned c:3;
}str1;
struct STR2 {
unsigned b1:5;
unsigned b2:2;
}str2;
};
int main (void)
{
union share m_struct ;
m_struct.val = 0x99;
printf("%d\n",m_struct.str1.a);//001
printf("%d\n",m_struct.str1.b);//11
printf("%d\n",m_struct.str1.c);//100
printf("%d\n",m_struct.str2.b1);//110001
printf("%d\n",m_struct.str2.b2);//00
return 0;
}