二进制有多少个0

#include <iostream>using namespace std;//古有求二进制数中1的个数,今有求二进制中0的个数。

int Grial(int x){

int count = 0;

while (x + 1)

{

count++;

x |= (x + 1);

}

return count;

}int main()

{

cout << Grial(1) << endl;

return 0;

}//为了方便验证,我把求二进制数中1的个数也写下来。

#include <iostream>using namespace std;

int Grial(int x){ int count = 0;

while (x)

{

count++;

x &= (x - 1);

}

return count;

}

int main(){

cout << Grial(3) << endl;

return 0;

}

原文地址 http://blog.csdn.net/liuhuiyan_2014/article/details/47384737

猜你喜欢

转载自blog.csdn.net/sinat_34259781/article/details/79421267
今日推荐