有效地设置数字 N 中的 X 位,从 MSB 的位置 P 开始
Effectively set X-bits in a number N, starting from position P from MSB
下面的代码在数字N中设置X位从LSB开始位置P(N是32位数字)
//gcc 5.4.0
#include <stdio.h>
unsigned int set_bits_pos(int N, int X, int P)
{
unsigned int mask = ((1UL << X)-1) << P;
return (N|mask);
}
int main(void)
{
unsigned int bits = 3, pos=5, num=0x0F;
printf("0x%X\n", set_bits_pos(num,bits,pos));
return 0;
}
输出:
0xEF
如何将上述函数转换为设置数字N中的X位,从MSB开始的位置P?最好的方法。谢谢
编辑:根据建议从 MSB 和 Arch Independent 设置 32 位和 64 位 N
//gcc 5.4.0
#include <stdio.h>
size_t set_bits_pos(size_t N, int X, int P)
{
size_t mask = ((1UL << X)-1) << ((8*sizeof(N)) - P - X);
return (N|mask);
}
int main(void)
{
size_t num=0x0F;
int bits = 3, pos=5;
printf("0x%X\n", set_bits_pos(num,bits,pos));
return 0;
}
输出:
0x0700000F
我建议使用精确宽度的整数类型:
#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
是要跳过的 low/high 位数(P
有问题)。
下面的代码在数字N中设置X位从LSB开始位置P(N是32位数字)
//gcc 5.4.0
#include <stdio.h>
unsigned int set_bits_pos(int N, int X, int P)
{
unsigned int mask = ((1UL << X)-1) << P;
return (N|mask);
}
int main(void)
{
unsigned int bits = 3, pos=5, num=0x0F;
printf("0x%X\n", set_bits_pos(num,bits,pos));
return 0;
}
输出:
0xEF
如何将上述函数转换为设置数字N中的X位,从MSB开始的位置P?最好的方法。谢谢
编辑:根据建议从 MSB 和 Arch Independent 设置 32 位和 64 位 N
//gcc 5.4.0
#include <stdio.h>
size_t set_bits_pos(size_t N, int X, int P)
{
size_t mask = ((1UL << X)-1) << ((8*sizeof(N)) - P - X);
return (N|mask);
}
int main(void)
{
size_t num=0x0F;
int bits = 3, pos=5;
printf("0x%X\n", set_bits_pos(num,bits,pos));
return 0;
}
输出:
0x0700000F
我建议使用精确宽度的整数类型:
#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
是要跳过的 low/high 位数(P
有问题)。