您的位置:首页 > 其它

【oj1947】 求int型数据在内存中存储时1的个数

2014-07-03 15:44 232 查看
[1] 输入一个int型数据,计算出该int型数据在内存中存储时1的个数。

</pre><pre code_snippet_id="415390" snippet_file_name="blog_20140703_3_4965486" name="code" class="cpp">#include <iostream>
#include <string>
using namespace std;
 

int main()
{
	int x;
	cin>>x;
	int num = 0;
	if(x>=0)
	{
		while(x)
		{
			num += (x & 0x0001);
			x >>= 1;
		}
		cout<<num<<endl;
	}
	else
	{
		x=~x;
		while(x)
		{
			num += (x & 0x0001);
			x >>= 1;
		}
		cout<<32-num<<endl;
	}
    system("pause");
 
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐