惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

N
News and Events Feed by Topic
爱范儿
爱范儿
Apple Machine Learning Research
Apple Machine Learning Research
博客园 - 叶小钗
Last Week in AI
Last Week in AI
博客园 - 三生石上(FineUI控件)
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
月光博客
月光博客
大猫的无限游戏
大猫的无限游戏
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - Franky
人人都是产品经理
人人都是产品经理
The Cloudflare Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 司徒正美
罗磊的独立博客
博客园 - 聂微东
T
Troy Hunt's Blog
美团技术团队
IT之家
IT之家
A
Arctic Wolf
腾讯CDC
雷峰网
雷峰网
SecWiki News
SecWiki News
博客园_首页
L
LINUX DO - 最新话题
Cloudbric
Cloudbric
量子位
N
News and Events Feed by Topic
小众软件
小众软件
C
CXSECURITY Database RSS Feed - CXSecurity.com
Cyberwarzone
Cyberwarzone
J
Java Code Geeks
V
V2EX
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Latest news
Latest news
Webroot Blog
Webroot Blog
F
Fortinet All Blogs
P
Privacy International News Feed
NISL@THU
NISL@THU
Google Online Security Blog
Google Online Security Blog
WordPress大学
WordPress大学
PCI Perspectives
PCI Perspectives
GbyAI
GbyAI
宝玉的分享
宝玉的分享
阮一峰的网络日志
阮一峰的网络日志
S
Secure Thoughts
Simon Willison's Weblog
Simon Willison's Weblog
P
Palo Alto Networks Blog
V
Visual Studio Blog

博客园 - woodfish

偏序集的Dilworth定理 XJOJ历经2个月终于完成啦~~ fork()调用的一个趣题 实现google那种输入框提示的功能 POJ 3691 安徽第二题 有限状态自动机+DP 哈尔滨赛区网络预选赛总结 [计算几何]点集中的点能组成多少个正方形 [计算几何]POJ 1375 点对圆的切线+线段重叠 [计算几何]POJ 1556 判断线段相交+Dijkstra [计算几何] POJ 1873 暴力+凸包 [计算几何]POJ 1266 三角形的外接圆 圆的参数方程 POJ 1026 置换群 [计算几何]POJ 1031 计算点对多边形的偏转角度 [计算几何]POJ2079 求点集中面积最大的三角形 [计算几何]POJ3608 求2个不相交凸包的最短距离 [计算几何]凸包的旋转卡壳算法 C++禁止一个类被继承的技术 C与汇编的接口技术 避免使用条件分支
计算位数的3种方法
woodfish · 2008-02-29 · via 博客园 - woodfish

方法1:

int count_bits(unsigned int data) {
  
int cnt=0;
  
  
while(data!=0{
    data
=data&(data-1);
    cnt
++;
  }

  
return cnt;
}


方法2:(为一个字节打表)

static unsigned char byte_bit_count[256];

void initialize_count_bits() {
  
int cnt,i,data;
  
for(i=0;i<256;i++{
    cnt
=0;
    data
=i;
    
while(data!=0{
      data
=data&(data-1);
      cnt
++;
    }

    byte_bit_count[i]
=cnt;
  }

}


int count_bits(unsigned int data)
{
  
const unsigned char *byte=(unsigned char*)&data;
  
return byte_bit_count[byte[0]]+byte_bit_count[byte[1]]+
           byte_bit_count[
byte[2]]+byte_bit_count[byte[3]];
}

方法3:(计算一个字种所有位(0,1)的和)

int count_bits(unsigned int x)
{
  
static unsigned int mask[]={0x55555555,
                                           
0x33333333,
                                           
0x0F0F0F0F,
                                           
0x00FF00FF,
                                           
0x0000FFFF}
;
  
int i;
  
int shift;

  
for(i=0,shift=1;i<5;i++,shift*=2)
    x
=(x&mask[i])+((x>>shift)&mask[i]);
  
return x;
}