[cpp] view plain copy // 判断一个整数的二进制位中有多少个1 void totalOne(int x) { int count = 0; while(x) { x = x & ( x - 1 ); count++; } printf("count = %d/n", count); } 循环: x = x & ( x - 1 ); count++; 直到x为0为止。该方法的时间复杂度是O(m)