以下给出计算代码:
<pre name="code" class="cpp">#include <iostream> using namespace std; int foo(int x) { int count = 0; while(x) { count++; x = x&(x - 1); } return count; } int main() { cout << foo(500) << endl; return 0; }
举一反三 怎样推断一个数是不是2的N次方, 。(x&(x-1)
以下给出计算代码:
<pre name="code" class="cpp">#include <iostream> using namespace std; int foo(int x) { int count = 0; while(x) { count++; x = x&(x - 1); } return count; } int main() { cout << foo(500) << endl; return 0; }