参考文章:http://www.cnblogs.com/graphics/archive/2010/06/21/1752421.html

最简单的方法:

  1. int BitCount0(unsigned int n)
  2. {
  3. unsigned int c = ; // 计数器
  4. while (n >)
  5. {
  6. if((n &) ==) // 当前位是1
  7. ++c ; // 计数器加1
  8. n >>= ; // 移位
  9. }
  10. return c ;
  11. }

消除统计法

  1. int BitCount2(unsigned int n)
  2. {
  3. unsigned int c = ;
  4. for (c =; n; ++c)
  5. {
  6. n &= (n -) ; // 清除最低位的1
  7. }
  8. return c ;
  9. }

8bit查表法

  1. unsigned int table[] =
  2. {
  3. , , , , , , , , , , , , , , , ,
  4. , , , , , , , , , , , , , , , ,
  5. , , , , , , , , , , , , , , , ,
  6. , , , , , , , , , , , , , , , ,
  7. , , , , , , , , , , , , , , , ,
  8. , , , , , , , , , , , , , , , ,
  9. , , , , , , , , , , , , , , , ,
  10. , , , , , , , , , , , , , , , ,
  11. , , , , , , , , , , , , , , , ,
  12. , , , , , , , , , , , , , , , ,
  13. , , , , , , , , , , , , , , , ,
  14. , , , , , , , , , , , , , , , ,
  15. , , , , , , , , , , , , , , , ,
  16. , , , , , , , , , , , , , , , ,
  17. , , , , , , , , , , , , , , , ,
  18. , , , , , , , , , , , , , , , ,
  19. };
  20. int BitCount2(unsigned int n)
  21. {
  22. char * b = (char *)&n;
  23. return b[]+b[]+b[]+b[];
  24. }

巧妙转换法

  1. int BitCount3(unsigned int n)
  2. {
  3. n = (n &0x55555555) + ((n >>) &0x55555555) ;
  4. n = (n &0x33333333) + ((n >>) &0x33333333) ;
  5. n = (n &0x0f0f0f0f) + ((n >>) &0x0f0f0f0f) ;
  6. n = (n &0x00ff00ff) + ((n >>) &0x00ff00ff) ;
  7. n = (n &0x0000ffff) + ((n >>) &0x0000ffff) ;
  8.  
  9. return n ;
  10. }
  1.  

#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

  1. int popcount_2(UINT32 x)
  2. {
  3. x -= (x >> ) & m1; //put count of each 2 bits into those 2 bits
  4. x = (x & m2) + ((x >> ) & m2); //put count of each 4 bits into those 4 bits
  5. x = (x + (x >> )) & m4; //put count of each 8 bits into those 8 bits
  6. x += x >> ; //put count of each 16 bits into their lowest 8 bits
  7. x += x >> ; //put count of each 32 bits into their lowest 8 bits
  8. return x & 0x1f;
  9. }
  1. inline short popcount_3(UINT32 x)
  2. {
  3. x -= (x >> ) & m1; //put count of each 2 bits into those 2 bits
  4. x = (x & m2) + ((x >> ) & m2); //put count of each 4 bits into those 4 bits
  5. x = (x + (x >> )) & m4; //put count of each 8 bits into those 8 bits
  6. return (x * h01) >> ; // left 8 bits of x + (x<<8) + (x<<16) + (x<<24)
  7. }
    //除了指令法,这种最快

指令法

  1. //sse - 4,编译时加入 -msse4[相当于4.1 + 4.2]
    #include<nmmintrin.h>
    unsigned int n = ;
  2. 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. 读取、写入excel数据

    在实际项目中,不可避免的会操作excel表格.一直以来都是读取excel表格,可今天为了写入excel表格,可是煞费苦心,终于完成,记录下来以便后续使用. 1.读取excel表格的数据 读取excel ...

  2. OC面向对象—多态

    OC面向对象—多态 一.基本概念 多态是基于继承的基础之上的,多态可以使得父类的指针指向子类的对象.如果函数或参数中使用的是父类类型,可以传入父类.子类对象,但是父类类型的变量不能直接调用子类特有的方 ...

  3. echarts html传参+js请求+ashx服务 代码方式

    html 头传参方式 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <hea ...

  4. oracle修改sys用户密码

  5. Linux脚本实现“按任意键继续/Press any key to continue”效果

    此代码来自lnmp一键安装包,用于实现“按任意键继续/Press any key to continue”效果: get_char() { SAVEDSTTY=`stty -g` #隐藏终端输入显示 ...

  6. Oracle Flashback Technologies - 闪回被drop的表

    Oracle Flashback Technologies - 闪回被drop的表 在oracle10g中,drop一个表后,表没有真正被删除,支持被rename后放在recyclebin中. #新建 ...

  7. 怎么设置task的最大线程数

    //-------------------------------------------------------------------------- // // Copyright (c) Mic ...

  8. Swift动画编程指南-01 简介

    大家好,我是老镇,这段时间家里和工作上发生了很多的事情,所以很长一段时间都没有出来搞什么小动作了.在接下来的一段时间内我会制作一些列关于使用Swift进行动画编程的视频,希望和大家胃口. 在iOS的世 ...

  9. SWF类标准开头Tag

    [SWF(width="800", height="600", backgroundColor="#ffffff", frameRate=& ...

  10. Codeforce Round #217 Div2

    e,妈蛋,第二题被hack了 没理解清题意,- -居然也把pretest过了,- -# A: 呵呵! B:包含任意一个子集的输出NO!,其他输出YES! C:贪心额,类似上次的Topcoder的500 ...