分析:把一个整数减去1,再和原整数做与运算,会把该整数最右边的一个1变为0,那么一个整数中有多少个1,就能进行多少次这样的操作。
int numberof1(int n) { int count=0; while(n) { ++count; n=(n-1)&n; } return count; }