reference:
http://www.geeksforgeeks.org/turn-off-the-rightmost-set-bit/
Problem Definition:
Write a C function that unsets the rightmost set bit of an integer.
Solution:
Let the input number be n. n-1 would have all the bits flipped after the rightmost set bit (including the set bit). So, doing n&(n-1) would give us the required result.
Code:
/* unsets the rightmost set bit of n and returns the result */
int fun(unsigned int n)
{
return n&(n-1);
}
1017

被折叠的 条评论
为什么被折叠?



