设计一个函数,找出一个32位的整形二进制数中的1的个数,尽量不要使用逐位比较

华亭真人 2009-08-10 11:06:15


#include<iostream>
using namespace std;
void main()
{

int i=1023;
int n=0;
while(i)
{
i<<=1;
if(i<0)
n++;
}

cout<<n<<endl;
}


我是这样写的,抛砖引玉。。
...全文
677 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
Damn_boy 2009-08-11
  • 打赏
  • 举报
回复
Gameloft?
专门作手机游戏的公司?
kakashi0309 2009-08-11
  • 打赏
  • 举报
回复

#include<iostream>
using namespace std;
void main()
{

int i=1023;
int n=0;
while(i)
{
i &= i-1;
n++;
}

cout<<n<<endl;
}
pimeson 2009-08-11
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 baihacker 的回复:]
C/C++ code#include<iostream>usingnamespace std;int main()
{int i=1023;int cnt=0;for (; i; i&= i-1,++cnt);
cout<< cnt<< endl;return0;
}
[/Quote]

厉害啊~~~
pimeson 2009-08-11
  • 打赏
  • 举报
回复
楼主的答案有问题,如果为负数,则少算了一个1,应该将 i<<=1;
放在 if(i<0)
n++;
下面
dfkjsdhfks 2009-08-11
  • 打赏
  • 举报
回复
mark
abcdef0966 2009-08-11
  • 打赏
  • 举报
回复
学习了,觉得最重要的是两点
1、比较次数
2、考虑负数
华亭真人 2009-08-11
  • 打赏
  • 举报
回复
学习了
YFLK 2009-08-11
  • 打赏
  • 举报
回复
用移位操作最简单!
bfhtian 2009-08-10
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 baihacker 的回复:]
C/C++ code#include<iostream>usingnamespace std;int main()
{int i=1023;int cnt=0;for (; i; i&= i-1,++cnt);
cout<< cnt<< endl;return0;
}
[/Quote]
学习了
adventurelw 2009-08-10
  • 打赏
  • 举报
回复
bitset<32> b(n);
b.count();


或者
n & 0xF:0有0个1,;1、2、4、8有1个1;3、5、6、9、A、C有2个1;7、B、D、E有3个1;F有4个1
以上为最低4位
n & 0xF0:索引4-7位
n & 0xF00:索引8-11位
需要八次循环,每次四位。
FancyMouse 2009-08-10
  • 打赏
  • 举报
回复
hacker's delight还没普及么
华亭真人 2009-08-10
  • 打赏
  • 举报
回复
。。。
mstlq 2009-08-10
  • 打赏
  • 举报
回复
#include<iostream>
using namespace std;
我也扔一砖头……
int main()
{

unsigned int i=1023;
int n=0;
while(i)
{
n += i&1;
i>>=1;
}
cout<<n<<endl;
return 0;
}
baihacker 2009-08-10
  • 打赏
  • 举报
回复

char table16[16] = { 0,1,1,2, 1,2,2,3, 1,2,2,3, 2,3,3,4} ;
char table256[256];

void SetData();

unsigned int bitcount32_1(unsigned int x);
unsigned int bitcount32_2(unsigned int x);
unsigned int bitcount32_3(unsigned int x);
unsigned int bitcount32_4(unsigned int x);
unsigned int bitcount32_1(unsigned int x)
{
x = (x & 0x55555555UL) + ((x >> 1) & 0x55555555UL);
x = (x & 0x33333333UL) + ((x >> 2) & 0x33333333UL);

x = (x & 0x0f0f0f0fUL) + ((x >> 4) & 0x0f0f0f0fUL);
x = (x & 0x00ff00ffUL) + ((x >> 8) & 0x00ff00ffUL);
x = (x & 0x0000ffffUL) + ((x >> 16) & 0x0000ffffUL);

return x;
}

unsigned int bitcount32_2(unsigned int x)
{
x = x - ((x >> 1) & 0x55555555UL);
x = (x & 0x33333333UL) + ((x >> 2) & 0x33333333UL);

x = (x + (x >> 4)) & 0x0f0f0f0fUL;
x += x >> 8;
x += x >> 16;

return x & 0x3f;
}

unsigned int bitcount32_3(unsigned int x)
{
return table16[x&0xf]+table16[(x>>4)&0xf]+table16[(x>>8)&0xf]+table16[(x>>12)&0xf] +
table16[(x>>16)&0xf]+table16[(x>>20)&0xf]+table16[(x>>24)&0xf]+table16[(x>>28)&0xf];
}

unsigned int bitcount32_4(unsigned int x)
{
return table256[x&0xff] + table256[(x>>8)&0xff]+
table256[(x>>16)&0xff] + table256[(x>>24)&0xff];
}

void SetData()
{
for (int i = 0; i < 16; i++)
for (int j = 0; j < 16; j++)
table256[i*16+j] = table16[i] + table16[j];
}
华亭真人 2009-08-10
  • 打赏
  • 举报
回复
今天去GAMELOFT 做的一个笔试题。。
baihacker 2009-08-10
  • 打赏
  • 举报
回复
#include <iostream>
using namespace std;


int main()
{
int i = 1023;
int cnt = 0;
for (; i; i &= i-1, ++cnt);
cout << cnt << endl;
return 0;
}
Z782282738 2009-08-10
  • 打赏
  • 举报
回复
你找它干吗?没事情做睡觉。

64,639

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧