C常用位操作相关宏定义

/* 位操作相关定义 */
/* 1. 获取指定位数掩码 */
#define BITS_MSK_GET(bitsoff, bits) (((0x01 << (bits)) - 1) << (bitsoff))
/* 2. 清除指定N BITS */
#define BITS_CLR(var,bitsoff,bits) ((var) & (~(BITS_MSK_GET(bitsoff,bits))))
/* 3. 获取指定 N BITS 值 */
#define BITS_GET(var,bitsoff,bits) ((var) & (BITS_MSK_GET(bitsoff,bits)))
/* 4. 获取指定N BITS 值并右对齐 */
#define BITS_RIGHT_JUST_GET(var,bitsoff,bits) \
((BITS_GET(var,bitsoff,bits) >> (bitsoff)) & BITS_MSK_GET(0,bits))
/* 5. 给变量指定BITS赋值 */
#define BITS_VAL_SET(var,bitsval,bitsoff,bits) \
(var = BITS_CLR(var,bitsoff,bits) | (((bitsval) << (bitsoff)) & BITS_MSK_GET(0,bits)))


你可能感兴趣的:(C编程)