数32位 unsigned int中1的个数
参考文章:http://www.cnblogs.com/graphics/archive/2010/06/21/1752421.html
最简单的方法:
int BitCount0(unsigned int n)
{
unsigned int c = ; // 计数器
while (n >)
{
if((n &) ==) // 当前位是1
++c ; // 计数器加1
n >>= ; // 移位
}
return c ;
}
消除统计法
int BitCount2(unsigned int n)
{
unsigned int c = ;
for (c =; n; ++c)
{
n &= (n -) ; // 清除最低位的1
}
return c ;
}
8bit查表法
unsigned int table[] =
{
, , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , ,
};
int BitCount2(unsigned int n)
{
char * b = (char *)&n;
return b[]+b[]+b[]+b[];
}
巧妙转换法
int BitCount3(unsigned int n)
{
n = (n &0x55555555) + ((n >>) &0x55555555) ;
n = (n &0x33333333) + ((n >>) &0x33333333) ;
n = (n &0x0f0f0f0f) + ((n >>) &0x0f0f0f0f) ;
n = (n &0x00ff00ff) + ((n >>) &0x00ff00ff) ;
n = (n &0x0000ffff) + ((n >>) &0x0000ffff) ; return n ;
}
#include <stdio.h>
typedef unsigned int UINT32;
const UINT32 m1 = 0x55555555; // 01010101010101010101010101010101
const UINT32 m2 = 0x33333333; // 00110011001100110011001100110011
const UINT32 m4 = 0x0f0f0f0f; // 00001111000011110000111100001111
const UINT32 m8 = 0x00ff00ff; // 00000000111111110000000011111111
const UINT32 m16 = 0x0000ffff; // 00000000000000001111111111111111
const UINT32 h01 = 0x01010101; // the sum of 256 to the power of 0, 1, 2, 3
int popcount_2(UINT32 x)
{
x -= (x >> ) & m1; //put count of each 2 bits into those 2 bits
x = (x & m2) + ((x >> ) & m2); //put count of each 4 bits into those 4 bits
x = (x + (x >> )) & m4; //put count of each 8 bits into those 8 bits
x += x >> ; //put count of each 16 bits into their lowest 8 bits
x += x >> ; //put count of each 32 bits into their lowest 8 bits
return x & 0x1f;
}
inline short popcount_3(UINT32 x)
{
x -= (x >> ) & m1; //put count of each 2 bits into those 2 bits
x = (x & m2) + ((x >> ) & m2); //put count of each 4 bits into those 4 bits
x = (x + (x >> )) & m4; //put count of each 8 bits into those 8 bits
return (x * h01) >> ; // left 8 bits of x + (x<<8) + (x<<16) + (x<<24)
}
//除了指令法,这种最快
指令法
//sse - 4,编译时加入 -msse4[相当于4.1 + 4.2]
#include<nmmintrin.h>
unsigned int n = ;
unsigned int bitCount = _mm_popcnt_u32(n) ;
关于sse有一个很好的学习资料,各个sse版本里的函数及其功能!http://blog.csdn.net/fengbingchun/article/details/18460199
数32位 unsigned int中1的个数的更多相关文章
- 面试题-----按位翻转32位unsigned
// test.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> #include < ...
- <转>32位移植到64位 注意事项
32bit-64bit porting work注意事项 64位服务器逐步普及,各条产品线对64位升级的需求也不断加大.在本文中,主要讨论向64位平台移植现有32位代码时,应注意的一些细小问题. 什么 ...
- c语言1左移32位(1<<32)是多少,左移-1位呢
C语言中 << 是逻辑移位,不是循环移位.1 左移 32 位后为 0,左移 -1 位实际是左移 255 位(互补),当然也是0.这种问题可以写一段小程序,单步执行,看一下每一步的结果.先说 ...
- dll文件32位64位检测工具以及Windows文件夹SysWow64的坑
自从操作系统升级到64位以后,就要不断的需要面对32位.64位的问题.相信有很多人并不是很清楚32位程序与64位程序的区别,以及Program Files (x86),Program Files的区别 ...
- 查看linux版本时32位的还是64位的
一. [root@wuy2 etc]# getconf LONG_BIT [root@wuy2 etc]# getconf WORD_BIT (32位的系统中int类型和long类型一般都是4字节,6 ...
- 对所有CPU寄存器的简述(16位CPU14个,32位CPU16个)
32位CPU所含有的寄存器有:4个数据寄存器(EAX.EBX.ECX和EDX)2个变址和指针寄存器(ESI和EDI)2个指针寄存器(ESP和EBP)6个段寄存器(ES.CS.SS.DS.FS和GS)1 ...
- dll文件32位64位检测工具以及Windows文件夹SysWow64的坑(很详细,还有自动动手编程探测dll)
阅读目录 dll文件不匹配导致数据库无法启动 究竟是System32还是SysWow64 区分dll文件32位64位的程序让我倍感迷惑 再次判断究竟是System32还是SysWow64——意想不到的 ...
- 32位Intel CPU所含有的寄存器
4个数据寄存器(EAX.EBX.ECX和EDX)2个变址和指针寄存器(ESI和EDI) 2个指针寄存器(ESP和EBP)6个段寄存器(ES.CS.SS.DS.FS和GS)1个指令指针寄存器(EIP) ...
- Linux:使用rpcgen实现64位程序调用32位库函数
摘要:本文介绍使用rpcgent实现64位程序调用32位库函数的方法,并给出样例代码. 我的问题 我的程序运行在64位Linux系统上,需要使用一个从外部获得的共享库中的函数,这个共享库是32位的,无 ...
随机推荐
- 改变DataGrid某一行和单元格的颜色
前段时间做WPF项目,需要改变DataGrid某一行的颜色.高度,以及某个单元格的颜色.单元格字体的颜色,自然就必需取到datagrid的一行和一行的单元格,网上也是搜索了好久才找到,记录下来便于使用 ...
- Speed-BI数据分析案例:2016年8月汽车销量排行榜
据中国汽车工业协会统计分析,2016年8月,乘用车市场表现较好,当月销量环比和同比均呈较快增长.1-8月,乘用车销量总体呈稳定增长,增幅比1-7月继续小幅提升. 8月,乘用车共销售179.5 ...
- Maven 命令操作项目
1.创建一个多模块的Java项目 shift+鼠标右键 创建项目命令: 旧版: mvn archetype:create -DgroupId=com.qhong -DartifactId=MavenP ...
- 使用Fiddler对android应用抓包
工作原理 先上个图 此图一目了然,可以看出fiddler在请求中所处的位置,我们就可以确定它能干些什么. 它实际工作在本机的8888端口http代理,我们启动fiddler时,它会自动更改代理设置: ...
- MVC4之ModelBinder-模型绑定
最近悟出来一个道理,在这儿分享给大家:学历代表你的过去,能力代表你的现在,学习代表你的将来. 十年河东十年河西,莫欺少年穷 学无止境,精益求精 最近在做自学MVC,遇到的问题很多,索性一点点总结 ...
- javascript 函数参数之中的undefined(zz)
开始看到很多js函数里都带一个undefined的参数,很是疑惑,后来查了查,原来是这样.假如我们定义了一个函数function a(){ if(arg1===undefined) alert(&q ...
- ads 的一些错误
遇到动不动就有*.o文件找不到的情况,而且通常都是开始的时候可以正常LINK,而经过若干次重新LINK后却找不到(即使只是在代码中加一个无关紧要得空格),新建个工程,将目前的.C和.H文件原封不动加进 ...
- 通过zabbix自带模板监控windowsPC机器
通过zabbix自带模板监控windowsPC机器 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 欢迎加入:高级运维工程师之路 598432640 相信有很多 ...
- IOS Suppot Font 苹果默认支持的字体一览2(普通,加粗,倾斜)
- __int64和long long输入输出
__int64 num; scanf("%I64d", &num); printf("%I64d\n", num); long long num; sc ...