码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode "Number of 1 Bits"

时间:2015-03-12 06:22:18      阅读:118      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!