数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位的,无 ...
随机推荐
- 项目重新部署后报The attribute required is undefined for the annotation type XmlElementRef
在另外一台机器上部署项目,项目导进Eclipse中发现有异常 public class BooleanFeatureType extends FeatureBaseType{ @XmlElementR ...
- sublime 垂直编辑
http://www.sublimetext.com/docs/2/multiple_selection_with_the_keyboard.html
- HTML是什么
HTML(Hyper Text Mark-up Language )即超文本标记语言,是 WWW 的描述语言,由 Tim Berners-lee提出.设计 HTML 语言的目的是为了能把存放在一台电脑 ...
- Oracle RAC 11.2.0.4 – RHRL 6.4: DiskGroup resource are not running on nodes. Database instance may not come up on these nodes
使用DBCA创建新库的时候报错: 查看资源状态: $ crsctl stat res -t ------------------------------------------------------ ...
- 类似 select 选择框效果及美化
网上有各种各样的关于 select 选择框的美化,找了很多,并没有好的样式效果.所以就找了一个利用 ul li 做的类似 select 选择框的效果,不废话了,先上图,效果如下: 对于上图的箭头效果, ...
- Velocity(1)——注释
Velocity的单行注释,使用## 多行注释使用#* cooments *#
- 20145207 《Java程序设计》第8周学习总结
前言: 这两天电路焊小车,有意思归有意思,确实挺忙的.博客到现在才写.执勤看的东西忘得好快呀,莫名的记不住.不说废话了,开始. 教材学习内容总结: 一.NIO和NIO2 1.NIO的定义 InputS ...
- ZOJ 3545 Rescue the Rabbit(AC自动机+状压DP)(The 2011 ACM-ICPC Asia Dalian Regional Contest)
Dr. X is a biologist, who likes rabbits very much and can do everything for them. 2012 is coming, an ...
- bzoj4137 [FJOI2015]火星商店问题
比较容易想到的做法是线段树套字典树,修改操作时在字典树上经过的节点维护一个最近被访问过的时间,这样询问操作只经过满足时间条件的节点,时间复杂度O(NlogN^2)但是因为线段树每个节点都要套个字典树, ...
- drds 分库表的创建速记
关键词 :dbpartition by hash(`INVESTOR_APPLY_ID`) 格式 :dbpartition by hash(分库字段) 创建例子: CREATE TABLE `BB_J ...