#include <stdio.h>
typedef struct {
unsigned char a:6;
unsigned char b:1;
unsigned char c:1;
} STRUCT_A;
int main ()
{
STRUCT_A struct_a;
struct_a.a = 0x3a;
struct_a.b = 0x1;
struct_a.c = 0x1;
printf ("sizeof (STRUCT_A) = %d\n", sizeof (STRUCT_A)); // 1
/* 0x3a 0x1 0x1 */
unsigned char *d;
d = (unsigned char *)&struct_a;
printf ("*d = 0x%x\n", (*d)&0x3F); // x86 *d = 0x3a
return 0;
}
#include <stdio.h>
typedef struct {
unsigned int a:11;
unsigned int b:5;
unsigned int c:16;
} STRUCT_A;
{
STRUCT_A struct_a;
struct_a.a = 0x55E;
struct_a.b = 0x16;
struct_a.c = 0x1;
printf ("sizeof (STRUCT_A) = %d\n", sizeof (STRUCT_A)); // 4
/* 0x55E 0x16 0x1 */
unsigned int *d;
d = (unsigned int *)&struct_a;
printf ("*d = 0x%x\n", (*d)); // x86 0x1b55e
printf ("*d = 0x%x\n", (*d)&0x7FF); // x86 0x55e
unsigned char *i;
i = (unsigned char *)&struct_a;
printf ("*i = 0x%x\n", *i); // 0x5e
/* 1 byte binary 0101 1110 */
/* 2 byte binary 10110 101 ==> 1011 0101 */
printf ("*(i+1) = 0x%x\n", *(i+1)); // 0xb5
return 0;
}
typedef struct {
unsigned char a:6;
unsigned char b:1;
unsigned char c:1;
} STRUCT_A;
int main ()
{
STRUCT_A struct_a;
struct_a.a = 0x3a;
struct_a.b = 0x1;
struct_a.c = 0x1;
printf ("sizeof (STRUCT_A) = %d\n", sizeof (STRUCT_A)); // 1
/* 0x3a 0x1 0x1 */
unsigned char *d;
d = (unsigned char *)&struct_a;
printf ("*d = 0x%x\n", (*d)&0x3F); // x86 *d = 0x3a
return 0;
}
#include <stdio.h>
typedef struct {
unsigned int a:11;
unsigned int b:5;
unsigned int c:16;
} STRUCT_A;
/*
c:16 b:5 a:11
0000 0000 0000 0001 10110 101 0101 1110
H L
c:16 b:5 a:11
0000 0000 0000 0001 1011 0101 0101 1110
H L
小端,获取4字节的数据从H开始读取
*/
{
STRUCT_A struct_a;
struct_a.a = 0x55E;
struct_a.b = 0x16;
struct_a.c = 0x1;
printf ("sizeof (STRUCT_A) = %d\n", sizeof (STRUCT_A)); // 4
/* 0x55E 0x16 0x1 */
unsigned int *d;
d = (unsigned int *)&struct_a;
printf ("*d = 0x%x\n", (*d)); // x86 0x1b55e
printf ("*d = 0x%x\n", (*d)&0x7FF); // x86 0x55e
unsigned char *i;
i = (unsigned char *)&struct_a;
printf ("*i = 0x%x\n", *i); // 0x5e
/* 1 byte binary 0101 1110 */
/* 2 byte binary 10110 101 ==> 1011 0101 */
printf ("*(i+1) = 0x%x\n", *(i+1)); // 0xb5
return 0;
}