整数的最右置位 [英] Off rightmost set bit of an integer
本文介绍了整数的最右置位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要关闭最右边的设置位.我的方法是找到最右边的位置,然后从该位置移开.我这样做是为了编写代码.
All I need to off the rightmost set bit. My approach is to find the position of right most bit, then off that bit. I write this code to do so..
int POS(int n)
{
int p=0;
while(n)
{
if(n%2==0)
{
p++;
}
else
{
break;
}
n=n/2;
}
return p;
}
int main(void)
{
int n=12;
int p = POS(n);
printf("%d \n", n&~(1<<p));
return 0;
}
有没有最简单的方法?
推荐答案
阅读 查看全文