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

推荐订阅源

Google DeepMind News
Google DeepMind News
B
Blog RSS Feed
量子位
aimingoo的专栏
aimingoo的专栏
V
Visual Studio Blog
Y
Y Combinator Blog
Vercel News
Vercel News
云风的 BLOG
云风的 BLOG
宝玉的分享
宝玉的分享
Engineering at Meta
Engineering at Meta
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
GbyAI
GbyAI
人人都是产品经理
人人都是产品经理
博客园 - 叶小钗
Stack Overflow Blog
Stack Overflow Blog
大猫的无限游戏
大猫的无限游戏
Microsoft Security Blog
Microsoft Security Blog
B
Blog
Last Week in AI
Last Week in AI
有赞技术团队
有赞技术团队
博客园 - 聂微东
腾讯CDC
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
J
Java Code Geeks

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