数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位的,无 ...
随机推荐
- SQL Server中的SQL语句优化与效率问题
很多人不知道SQL语句在SQL SERVER中是如何执行的,他们担心自己所写的SQL语句会被SQL SERVER误解.比如: select * from table1 where name='zhan ...
- Eclipse中添加Android系统jar包
这样做的好处是,可以使用Eclipse开发系统应用了,这样可以调用系统中才使用的API. 1.首先在项目中右击->属性.如图所示依次操作 2.添加User Library 3.第一次要新建Use ...
- Trace-导出已有的服务器端跟踪
跟踪(Trace)常被我们用来检查性能问题.通常我们会有针对CPU.Duration.Reads的创建跟踪定义,这一类的脚本一般不会包含太多的事件和列,筛选条件也相对简单.假如某一天你使用GUI定义了 ...
- python_字典
1. 字典的定义 字典由多个键及其对应的值构成(我们也把键/值对称为项).键是唯一的,值不唯一.键可以是数字.字符串甚至是元组. 2. 字典的创建 (1) phonebook = {"} 名 ...
- [PCL]1 PCL点云库安装
1.安装文件下载:官网,我还是比较喜欢别人编译好的安装包啊,哈哈. http://www.pointclouds.org/downloads/windows.html 2.傻瓜式安装(下面的依赖项都集 ...
- CNContact对通讯录的基本使用(增删改查)
/** * 注意:iOS9才有能使用 * 首先在工程里导入ContactsUI.framework和Contacts.framework两个框架 * * * 源代码的链接地址 * 链接: http:/ ...
- 安装和删除 Alcatraz 插件
在终端下输入如下命令来安装Alcatraz: curl -fsSL https://raw.github.com/supermarin/Alcatraz/master/Scripts/install. ...
- SWIFT UITableView的基本用法
import UIKit @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: ...
- Oracle修改时间报:ORA-01830: 日期格式图片在转换整个输入字符串之前结束的解决办法
1.错误原因: date类型不能包含秒以后的精度. 如日期:2010-01-01 20:02:20.0 解决方法:将日期秒以后的精度去除, to_date(substr(INVOICE_DATE,1, ...
- Java基础之集合框架——使用堆栈Stack<>对象模拟发牌(TryDeal)
控制台程序. public enum Rank { TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, A ...