- //因为如果某一位是1的话,那么减1再与,就会将该后面的所有值,包括该1全部置0,所以可以用while(x)
- int CountOne(int x)
- {
- int count = 0;
- while ( x )
- {
- count++;
- x = x & (x - 1);
- }
- return x;
- }
- //该片段来自于http://www.codesnippet.cn/detail/1712201411315.html
来源: http://www.codesnippet.cn/detail/1712201411315.html