联合体结和结构体以及几种小端模式下的高位字节的获取

发布于:2021-12-05 19:10:11

#include
#include
#include

#define highByte(x) ((uint8_t)((x)>>8))

typedef union test{
uint16_t aa;
uint8_t bb;
}myTest;

typedef struct str{
uint16_t AA;
uint32_t BB;
}myStruct;

typedef union number{
uint16_t value;
struct{
uint8_t low,high;
}half;
}number16_t;
uint8_t *getHighByte(uint16_t n)
{
uint8_t *ptr=(uint8_t *)&n;
return *(ptr+1);
}

int main()
{
uint16_t value=10;
myTest m;
myStruct mm;
long int aa_=&(m.aa);
long int bb_=&(m.bb);

uint16_t num=0xabcd;
number16_t num1={0xabcd};

long int AA_=&(mm.AA);
long int BB_=&(mm.BB);
printf("hello world!
");
printf("value=%d
",value);
printf("aa_=%ld,bb_=%ld
",aa_,bb_);
printf("AA_=%ld,BB_=%ld
",AA_,BB_);
printf("xx=%d,yy=%d
",sizeof(uint16_t),sizeof(uint32_t));

printf("1: highByte =%x
",highByte(num));
printf("2: highByte =%x
",getHighByte(num));
printf("3: highByte =%x
",num1.half.high);
}


相关推荐

最新更新

猜你喜欢