参考文章: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的个数的更多相关文章

  1. 面试题-----按位翻转32位unsigned

    // test.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> #include < ...

  2. <转>32位移植到64位 注意事项

    32bit-64bit porting work注意事项 64位服务器逐步普及,各条产品线对64位升级的需求也不断加大.在本文中,主要讨论向64位平台移植现有32位代码时,应注意的一些细小问题. 什么 ...

  3. c语言1左移32位(1<<32)是多少,左移-1位呢

    C语言中 << 是逻辑移位,不是循环移位.1 左移 32 位后为 0,左移 -1 位实际是左移 255 位(互补),当然也是0.这种问题可以写一段小程序,单步执行,看一下每一步的结果.先说 ...

  4. dll文件32位64位检测工具以及Windows文件夹SysWow64的坑

    自从操作系统升级到64位以后,就要不断的需要面对32位.64位的问题.相信有很多人并不是很清楚32位程序与64位程序的区别,以及Program Files (x86),Program Files的区别 ...

  5. 查看linux版本时32位的还是64位的

    一. [root@wuy2 etc]# getconf LONG_BIT [root@wuy2 etc]# getconf WORD_BIT (32位的系统中int类型和long类型一般都是4字节,6 ...

  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 ...

  7. dll文件32位64位检测工具以及Windows文件夹SysWow64的坑(很详细,还有自动动手编程探测dll)

    阅读目录 dll文件不匹配导致数据库无法启动 究竟是System32还是SysWow64 区分dll文件32位64位的程序让我倍感迷惑 再次判断究竟是System32还是SysWow64——意想不到的 ...

  8. 32位Intel CPU所含有的寄存器

    4个数据寄存器(EAX.EBX.ECX和EDX)2个变址和指针寄存器(ESI和EDI) 2个指针寄存器(ESP和EBP)6个段寄存器(ES.CS.SS.DS.FS和GS)1个指令指针寄存器(EIP) ...

  9. Linux:使用rpcgen实现64位程序调用32位库函数

    摘要:本文介绍使用rpcgent实现64位程序调用32位库函数的方法,并给出样例代码. 我的问题 我的程序运行在64位Linux系统上,需要使用一个从外部获得的共享库中的函数,这个共享库是32位的,无 ...

随机推荐

  1. CSRF verification failed. Request aborted.

    在使用Django提交Post表单时遇到如下错误: Forbidden (403) CSRF verification failed. Request aborted. 原因在"帮助&quo ...

  2. Android解决java.lang.OutOfMemoryError: bitmap size exceeds VM budget(转)

    昨天遇到这个问题就是从一个输入流里调用BitmapFactory.decodeStream(this.getContentResolver().openInputStream(uri))得到一个bit ...

  3. MEF 松耦合

    MEF天生就有解耦合的特性,虽然它不是为解耦而生,而主要是为插件类应用的开发而设计.如果主要是为了解除耦合的话可以使用IoC,Unity等. Unity 微软的IOC 代码: using System ...

  4. system执行shell命令

    system - execute a shell command #include <stdlib.h> int system (const char *command); 描述 syst ...

  5. popen&pclose管道方式操作shell命令

    popen, pclose - pipe stream to or from a process FILE *popen( const char *command, const char *type) ...

  6. UIPikerView的属性

    1.    numberOfComponents:返回UIPickerView当前的列数 NSInteger num = _pickerView.numberOfComponents; NSLog( ...

  7. mantis 安装问题

    1. 邮件 修改 config_defaults_inc.php $g_administrator_email = 'mantis@pooy.net'; $g_webmaster_email = 'm ...

  8. eclipse下部署web工程的两种方式

    习惯了Eclipse 的开发,就觉得不想那么懒去用MyEclipse傻瓜式的部署工程. 第一种,手动部署工程. 情况一:如果工程目录在tomcat目录的webapp目录下,这种情况就不需要有工程部署的 ...

  9. Java中几种日志方案

    .本文记录Java中几种常用的日志解决方案 0x01 Log4j .这应该是一个比较老牌的日志方案了,配置也比较简单,步骤如下 1)添加对应依赖,比如 Gradle 中 dependencies { ...

  10. Java基础(51):Super与this的区别

    1.     子类的构造函数如果要引用super的话,必须把super放在函数的首位. class Base { Base() { System.out.println("Base" ...