问题成功解决。。。。用上位运算就行啦~
程序代码:
#include <stdio.h> #pragma pack(1) struct file_data { unsigned int day:5; unsigned int month:4; unsigned int year:7; }; union Data { unsigned short data; struct file_data fd; }; #pragma pack() int main() { union Data Hi; unsigned short day = 30; unsigned short month = 12; unsigned short year = 1; Hi.data = day + (month << 5) + (year << 9); printf("%d/%d/%d\n", Hi.fd.year, Hi.fd.month, Hi.fd.day); return 0; }
原问题在这里:https://bbs.bccn.net/thread-355743-1-1.html