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

推荐订阅源

M
MIT News - Artificial intelligence
博客园 - Franky
H
Help Net Security
A
About on SuperTechFans
Know Your Adversary
Know Your Adversary
罗磊的独立博客
Help Net Security
Help Net Security
腾讯CDC
博客园 - 三生石上(FineUI控件)
月光博客
月光博客
Project Zero
Project Zero
有赞技术团队
有赞技术团队
Blog — PlanetScale
Blog — PlanetScale
T
Threat Research - Cisco Blogs
The Hacker News
The Hacker News
Engineering at Meta
Engineering at Meta
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Simon Willison's Weblog
Simon Willison's Weblog
T
Threatpost
Google DeepMind News
Google DeepMind News
V
V2EX
B
Blog
人人都是产品经理
人人都是产品经理
J
Java Code Geeks
N
Netflix TechBlog - Medium
P
Privacy International News Feed
Recorded Future
Recorded Future
D
Darknet – Hacking Tools, Hacker News & Cyber Security
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Stack Overflow Blog
Stack Overflow Blog
Cisco Talos Blog
Cisco Talos Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
S
Securelist
NISL@THU
NISL@THU
The GitHub Blog
The GitHub Blog
T
Troy Hunt's Blog
S
Security @ Cisco Blogs
Vercel News
Vercel News
L
LINUX DO - 热门话题
博客园_首页
The Register - Security
The Register - Security
GbyAI
GbyAI
TaoSecurity Blog
TaoSecurity Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
V2EX - 技术
V2EX - 技术
L
LangChain Blog
T
Tor Project blog
P
Privacy & Cybersecurity Law Blog
Security Latest
Security Latest
K
Kaspersky official 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;
}