我建议使用精确的宽度整数类型:
#include <inttypes.h>
uint32_t set_low32(const uint32_t value,
const unsigned int bits,
const unsigned int skip)
{
uint32_t mask = (~(uint32_t)0) >> (32 - bits);
return value | (mask << skip);
}
uint64_t set_low64(const uint64_t value,
const unsigned int bits,
const unsigned int skip)
{
uint64_t mask = (~(uint64_t)0) >> (64 - bits);
return value | (mask << skip);
}
uint32_t set_high32(const uint32_t value,
const unsigned int bits,
const unsigned int skip)
{
uint32_t mask = (~(uint32_t)0) << (32 - bits);
return value | (mask >> skip);
}
uint64_t set_high64(const uint64_t value,
const unsigned int bits,
const unsigned int skip)
{
uint64_t mask = (~(uint64_t)0) << (64 - bits);
return value | (mask >> skip);
}
bits
是要设置的位数(
X
有疑问),以及
skip
是要跳过的低/高位数(
P
有问题的。