我正在使用以下函数从数字中提取n位。我从here获得了此功能。为了方便起见,我将其发布在这里。我想获取0到9之间的位,然后在另一条语句中获取10到15位。我传入1033。我得到0到9位的正确值,但10到15位的值不正确。我应该改为1我收到1024条建议吗?
unsigned createMask(unsigned a, unsigned b)
{
unsigned r = 0;
for (unsigned i = a; i <= b; i++)
r |= 1 << i;
return r;
}
现在我有这个
unsigned short langId = 1033 ;// 10000001001
unsigned primary = createMask(0,9) & langId; //gives 9 correct
unsigned sec = createMask(10,15) & langId; //gives 1024 incorrect should be 1
答案 0 :(得分:1)