C语言将日期按位存储在无符号短整型中

#include <stdint.h>

uint16_t date2short (int day, int month, int year) {
    if (day > 31 || day < 1 || month > 12 || month < 1 || year > 127 || year < 0)
        return 0;
    uint16_t result = 0;
    result |= day << 11;
    result |= month << 7;
    result |= year;

    return result;
}

代码来源:https://stackoverflow.com/questions/71640383/bitwise-storing-of-a-date-in-an-unsigned-short-int-why-is-my-result-off-by-1

posted @ 2023-09-06 13:18  C羽言  阅读(18)  评论(0)    收藏  举报