例题1

#include<iostream>

using namespace std;

int func(int x){

  int count = 0;

  while(x){

    count++;

    x = x & (x - 1);

  }

  return count;

}

int main(){

  cout<<func(9999)<<endl;

  return 0;

}

答案:8

本题目func函数返回值是形参x转化的二进制包含1的数量9999--->10011100001111

posted @ 2015-04-19 15:53  原声  阅读(107)  评论(0)    收藏  举报