码迷,mamicode.com
首页 > 编程语言 > 详细

将一个数转化为二进制后,求其中1的个数 C++

时间:2015-04-25 16:29:27      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:

#include <iostream>
using namespace std;
int func(int x)
{
int count=0;
while(x)
{
x=x&(x-1);//与的次数就是包含1的个数
count++;
}
return count;
}

void main()
{
int x;
cin>>x;
cout<<func(x)<<endl;
}

将一个数转化为二进制后,求其中1的个数 C++

标签:

原文地址:http://www.cnblogs.com/alanhu/p/4455944.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!