求一数据二进制位中有几个1,怎样做最简便?

glory_manred 2010-07-20 11:39:11
如1010中有2个1,1011中有3个,请赐教
...全文
296 22 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
little_angel 2011-05-18
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 koukouwuwu 的回复:]
int GetNumFromBinary(int num)
{
int i = 0;

while( num )
{
num = num & ( num - 1 );//将该数二进制和该数减一的二进制一次相与
i++;
}
cout<<"the num of 1 is"<<i<<endl;

return i;
}
[/Quote]

好方法,综合效率应该很好的
dfasri 2011-05-18
  • 打赏
  • 举报
回复
用BYTE的最大值来定义一个数组, 进行查表就是了. 类似于:

int HashTable[0xFF]

HashTable[0] = 0; 二进制: 全零
HashTable[1] = 1; 二进制: 0000 0001
HashTable[2] = 1; 二进制: 0000 0010
HashTable[3] = 2; 二进制: 0000 0011

写得这么明显了, 看得明白吧?
richblueh 2011-05-18
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 jennyvenus 的回复:]
C/C++ code
int main(int argc, char* argv[])
{
int t = 0x11223344;
int i;
i = 0;

while( t )
{
t = t & ( t - 1 );
i++;
}

printf( "%d", i );……
[/Quote]

哪位大侠,讲解下,没看懂啊
xiaopoy 2011-05-18
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 3jaja 的回复:]
32位最值得学习的是这个方法
int test(int n)
{
n = (n&0x55555555) + ((n>>1)&0x55555555);
n = (n&0x33333333) + ((n>>2)&0x33333333);
n = (n&0x0f0f0f0f) + ((n>>4)&0x0f0f0f0f);
n = (n&……
[/Quote]

up #9
yf592039020 2011-05-18
  • 打赏
  • 举报
回复
#include <iostream>
#include <bitset>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
string str = "1101";
bitset<32> bit(str);
cout<<bit.count()<<endl;
return 0;
}


值得学习~~
有个频数检测的问题想请教
kyotrue 2011-05-18
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 3jaja 的回复:]
32位最值得学习的是这个方法
int test(int n)
{
n = (n&0x55555555) + ((n>>1)&0x55555555);
n = (n&0x33333333) + ((n>>2)&0x33333333);
n = (n&0x0f0f0f0f) + ((n>>4)&0x0f0f0f0f);
n = (n&……
[/Quote]
顶这个,前几天才看过一篇列举所有判断位为1的个数的方法的文章,这个确实最有效率,就是不推算一下的看不懂。
一名程序员 2010-07-22
  • 打赏
  • 举报
回复
#include <iostream>
#include <windows.h>
using namespace std;

int main()
{
int a = 0x12345678;
int length = sizeof(a) * 8;
int count = 0;
for ( int i = 0; i < length; i++)
{
if(a & 1)
count++;
a >>= 1;
}
cout << count << endl;

return 0;
}
Eleven 2010-07-21
  • 打赏
  • 举报
回复


#include <iostream>
#include <bitset>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
string str = "1101";
bitset<32> bit(str);
cout<<bit.count()<<endl;
return 0;
}
晨星 2010-07-21
  • 打赏
  • 举报
回复
看看偶的博客::)
http://blog.csdn.net/steedhorse/archive/2009/11/14/4809493.aspx
许文君 2010-07-21
  • 打赏
  • 举报
回复
按位与1即可。
ZhuoJieTech 2010-07-21
  • 打赏
  • 举报
回复
int GetNumFromBinary(int num)
{
int i = 0;

while( num )
{
num = num & ( num - 1 );//将该数二进制和该数减一的二进制一次相与
i++;
}
cout<<"the num of 1 is"<<i<<endl;

return i;
}
LZNJ06 2010-07-21
  • 打赏
  • 举报
回复
学习见识
3jaja 2010-07-21
  • 打赏
  • 举报
回复
32位最值得学习的是这个方法
int test(int n)
{
n = (n&0x55555555) + ((n>>1)&0x55555555);
n = (n&0x33333333) + ((n>>2)&0x33333333);
n = (n&0x0f0f0f0f) + ((n>>4)&0x0f0f0f0f);
n = (n&0x00ff00ff) + ((n>>8)&0x00ff00ff);
n = (n&0x0000ffff) + ((n>>16)&0x0000ffff);

return n;
}
向立天 2010-07-21
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 jennyvenus 的回复:]
C/C++ code
int main(int argc, char* argv[])
{
int t = 0x11223344;
int i;
i = 0;

while( t )
{
t = t & ( t - 1 );
i++;
}

printf( "%d", i );……
[/Quote]
值得学习
用户 昵称 2010-07-21
  • 打赏
  • 举报
回复
int main(int argc, char* argv[])
{
int t = 0x11223344;
int i;
i = 0;

while( t )
{
t = t & ( t - 1 );
i++;
}

printf( "%d", i );
return 0;
}


绝对最快!
汪宁宇 2010-07-21
  • 打赏
  • 举报
回复
扩充一下,求一个二进制的文件中所有值为0的字节数.
maple_zhj 2010-07-21
  • 打赏
  • 举报
回复
#include "stdafx.h"

int main(int argc, char* argv[])
{
int t_Value = 1+16+32 ;//0x110001

int nCount =0; //统计1的个位
while(t_Value)
{
t_Value &= t_Value - 1;
nCount++;
}
printf("Count = %d\n", nCount);

return 0;
}

跟楼上的一样。

详见 《编程之美》

原理是:

利用二个特性:
1、位与运算,有0即为0,则必有 两个数相与 1的个数 趋向于 递减。
2、2进制数的 减1 运算,必要借位,而且借的是最低的,并且位值为1的位,
   这时,将抹掉 最低位的1,
*、将被抹数 与原数 相与, 被抹位必为零。
  而比被抹数的被抹位更低的位,虽然为1,但是原数与之对应的位必为0,结果此位为0,

为什么? 原数与之对应的位必为0? 
请参考假设? 被抹位是 最低的,并且位值为1的位!!!

明白了吗?
yefan1020 2010-07-21
  • 打赏
  • 举报
回复
顶一下
ok1234567 2010-07-21
  • 打赏
  • 举报
回复
1、将0-255(一个字节的所有可能值)中的1的个数做成一个数组(256元素)
static BYTE an[256]={0,1,1,2...};
2、统计1的个数的问题就是扫描累加 sum += an[aBytes[i]];
klkvc386 2010-07-21
  • 打赏
  • 举报
回复
Mark.............
加载更多回复(1)

16,548

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • AIGC Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

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