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

推荐订阅源

L
LangChain Blog
The GitHub Blog
The GitHub Blog
Recent Announcements
Recent Announcements
MyScale Blog
MyScale Blog
P
Proofpoint News Feed
S
Security @ Cisco Blogs
N
News and Events Feed by Topic
H
Hacker News: Front Page
Attack and Defense Labs
Attack and Defense Labs
S
Secure Thoughts
Microsoft Security Blog
Microsoft Security Blog
N
Netflix TechBlog - Medium
U
Unit 42
Stack Overflow Blog
Stack Overflow Blog
T
Threat Research - Cisco Blogs
Google Online Security Blog
Google Online Security Blog
Spread Privacy
Spread Privacy
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
L
LINUX DO - 热门话题
T
Tenable Blog
博客园 - 叶小钗
D
DataBreaches.Net
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园_首页
人人都是产品经理
人人都是产品经理
aimingoo的专栏
aimingoo的专栏
C
Check Point Blog
博客园 - 三生石上(FineUI控件)
量子位
P
Proofpoint News Feed
H
Help Net Security
Blog — PlanetScale
Blog — PlanetScale
宝玉的分享
宝玉的分享
Recorded Future
Recorded Future
The Register - Security
The Register - Security
F
Fortinet All Blogs
Engineering at Meta
Engineering at Meta
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Last Week in AI
Last Week in AI
S
Schneier on Security
V
Vulnerabilities – Threatpost
雷峰网
雷峰网
Microsoft Azure Blog
Microsoft Azure Blog
G
GRAHAM CLULEY
G
Google Developers Blog
月光博客
月光博客
V
V2EX
T
Troy Hunt's Blog
A
Arctic Wolf

博客园 - 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;
}