首页 > 其他 > 详细

LeetCode "Number of 1 Bits"

时间:2015-03-12 06:20:16      阅读:486      评论:0      收藏:0      [点我收藏+]

Counting number of enabled bits in uint32_t.. Here is a O(lg32) solution. Very smart, like bottom-up parallel tree post-order traversal.

Solution from discussion of LC:

int hammingWeight(uint32_t n) 
{ n
= ((n >> (1 << 0)) & 0x55555555) + (n & 0x55555555); n = ((n >> (1 << 1)) & 0x33333333) + (n & 0x33333333); n = ((n >> (1 << 2)) & 0x0F0F0F0F) + (n & 0x0F0F0F0F); n = ((n >> (1 << 3)) & 0x00FF00FF) + (n & 0x00FF00FF); return ((n >> (1 << 4)) & 0xFFFF) + (n & 0xFFFF); }

 

LeetCode "Number of 1 Bits"

原文:http://www.cnblogs.com/tonix/p/4331405.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!