算中的整数集合的比特数 [英] Count the number of set bits in an integer
问题描述
可能重复:
<一href="http://stackoverflow.com/questions/109023/best-algorithm-to-count-the-number-of-set-bits-in-a-32-bit-integer">Best算法来计算一个32位整数集位数?
您好,
我在接受采访时碰到了这个问题。我想找到组的比特数中的给定数量以优化的方式。
I came across this question in an interview. I want to find the number of set bits in a given number in an optimized way.
例如:
如果给定的数字是7,然后输出应该是3(因为7的二进制是111,我们有三个1秒)
If the given number is 7 then output should be 3 (since binary of 7 is 111 we have three 1s)
如果给定的数字8,然后输出应该是1(因为8位二进制是1000,我们有一个1秒)
If the given number 8 then output should be 1 (since binary of 8 is 1000 we have one 1s)
我们需要找到一种优化方式1的个数。有什么建议么?
we need to find the number of ones in an optimized way. Any suggestions?
推荐答案
我觉得这有最好的算法在这里:
I think that this has the best algorithm here :
http://en.wikipedia.org/wiki/Hamming_weight
此外,一起来看看这个:
Also, take a look at this :
HTTP://compprog.word press.com / 2007/11/06 /二进制数计数位/
这篇关于算中的整数集合的比特数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!