所以这里是代码:
int create_mask(unsigned b, unsigned e)
{
unsigned int mask=1;
if(b<e || b<0 || e<0)
{
printf("Wrong values, starting bit can't be smaller than ending.\n");
printf("Both got to be >= 0.\n");
exit(EXIT_FAILURE);
}
while(b>0)
{
printf("%u\n", b);
mask<<=1;
if(b>e)
mask|=1;
b--;
}
return ~mask; /* negates mask for later purpose that is clearing corresponding bits */
}
函数为某些位操作创建掩码,但应采用两个无符号整数 b 和 e,均为非负数。问题是如何防止用户输入负数?当使用 (-1,0) 调用函数时,它会启动循环,并应退出并出现错误。