c++位操作

#define BIT_MASK_1 1
#define BIF_MASK_2 3
#define BIT_MASK_3 7
#define BIT_MASK_4 15
#define BIT_MASK_5 31

#define BIT_RESET(src, pos, mask) (src & (~(BIT_MASK_##mask << pos)))
#define BIT_SET(src, pos, mask, v) (BIT_RESET(src, pos, mask) | (v << pos))
#define BIT_GET(src, pos, mask) ((src & (BIT_MASK_##mask << pos)) >> pos)

int main(int argc, char* argv[]) {
    quint32 v = 0b01001101;

    quint32 temp = BIT_RESET(v, 1, 3);
    qDebug() << BIT_GET(temp, 1, 3);

    for (int i = 0; i <= 7; ++i) {
        quint32 temp = BIT_SET(v, 1, 3, i);
        qDebug() << BIT_GET(temp, 1, 3);
    }

    return 1;
}

点墨
26 声望3 粉丝

全栈前端开发工程师