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

推荐订阅源

Blog — PlanetScale
Blog — PlanetScale
Vercel News
Vercel News
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
量子位
Y
Y Combinator Blog
IT之家
IT之家
博客园 - 聂微东
L
LangChain Blog
爱范儿
爱范儿
H
Help Net Security
GbyAI
GbyAI
F
Fortinet All Blogs
B
Blog
Microsoft Security Blog
Microsoft Security Blog
罗磊的独立博客
C
Check Point Blog
博客园 - 三生石上(FineUI控件)
小众软件
小众软件
D
DataBreaches.Net
Last Week in AI
Last Week in AI
WordPress大学
WordPress大学
B
Blog RSS Feed
酷 壳 – CoolShell
酷 壳 – CoolShell
宝玉的分享
宝玉的分享

博客园 - octoberfirst

Service注册发现及其调用-分布式服务框架 比较成熟的支持数据分票的数据库产品 CodeCommit on Amazon AWS 使用yeoman来搭建webapp脚手架 5美元的互联网硬件主板 互联网创业的逻辑 Pyramid, Django, 和 Flask 51 Best DevOps Tools for #DevOps Engineers 目前的流行的一些开源框架 bank business Use HttpApplication.CompleteRequest Instead of Response.End A low-level Look at the ASP.NET Architecture Bit-tricks and other nifty little snippets. Endianess The best method for counting bits in a 32-bit integer(2) Scalability Best Practices: Lessons from eBay Changes to JavaScript, Part 1: EcmaScript 5 Enterprise Service Bus Overview Inheriting From a Native C++ Class in C# - octoberfirst
The best method for counting bits in a 32-bit integer
octoberfirst · 2010-03-13 · via 博客园 - octoberfirst
B[0] = 0x55555555 = 01010101 01010101 01010101 01010101
B[1] = 0x33333333 = 00110011 00110011 00110011 00110011
B[2] = 0x0F0F0F0F = 00001111 00001111 00001111 00001111
B[3] = 0x00FF00FF = 00000000 11111111 00000000 11111111
B[4] = 0x0000FFFF = 00000000 00000000 11111111 11111111

The best method for counting bits in a 32-bit integer v is the following:

v = v - ((v >> 1) & 0x55555555);                    // reuse input as temporary
v = (v & 0x33333333) + ((v >> 2) & 0x33333333); // temp
c = ((v + (v >> 4) & 0xF0F0F0F) * 0x1010101) >> 24; // count

http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel 












B[0] = 0x55555555 = 01010101 01010101 01010101 01010101





B[1] = 0x33333333 = 00110011 00110011 00110011 00110011





B[2] = 0x0F0F0F0F = 00001111 00001111 00001111 00001111





B[3] = 0x00FF00FF = 00000000 11111111 00000000 11111111





B[4] = 0x0000FFFF = 00000000 00000000 11111111 11111111















The best method for counting bits in a 32-bit integer v is the following:












v = v - ((v >> 1) & 0x55555555);                    // reuse input as temporary




v = (v & 0x33333333) + ((v >> 2) & 0x33333333);     // temp






c = ((v + (v >> 4) & 0xF0F0F0F) * 0x1010101) >> 24; // count 1





 01010101  01010101  01010101  01010101




v=

00000111




v >> 1


00000011




((v >> 1) & 0x55555555) 00000000 00000000 00000000 00000001





11111111 11111111 11111111 11111111




v = v - ((v >> 1) & 0x55555555);            10000000 00000000 00000000 00000110





00110011 00110011 00110011 00110011




 (v & 0x33333333) 00000000 00000000 00000000 00000010




 ((v >> 2) & 0x33333333);


00000001








00000011 0x1010101







 00000001000000010000000100000001






00000001000000010000000100000001

c = ((v + (v >> 4) & 0xF0F0F0F) * 0x1010101) >> 24; // count 00000011