C语言利用union 和 struct 进行位拆分

#include <stdlib.h>
typedef unsigned char      uint8_t;
union wate_temp{
    struct {
        uint8_t wate_temp_low   :1;
        uint8_t wate_temp_high  :4;
        uint8_t reverse            :3;
    } wate_temp_T;
    uint8_t wate_temp;
};

int main()
{
    union wate_temp wate_temp_u;
    wate_temp_u.wate_temp_T.wate_temp_high=7;
    wate_temp_u.wate_temp_T.wate_temp_low=1;
    printf("wate_temp=%d\n",wate_temp_u.wate_temp);
    wate_temp_u.wate_temp = 20;
    printf("wate_temp_high=%d\n", wate_temp_u.wate_temp_T.wate_temp_high);
    printf("wate_temp_low=%d\n",wate_temp_u.wate_temp_T.wate_temp_low);
    return(0);
}

 

posted @ 2022-06-20 17:02  bk街头狂舞  阅读(108)  评论(0)    收藏  举报