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

#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!\n");
   printf("value=%d\n",value);
   printf("aa_=%ld,bb_=%ld\n",aa_,bb_);
   printf("AA_=%ld,BB_=%ld\n",AA_,BB_);
   printf("xx=%d,yy=%d\n",sizeof(uint16_t),sizeof(uint32_t));

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

你可能感兴趣的:(C语言)