Duplicate:
Best algorithm to count the number of set bits in a 32-bit integer?
计算32位整数中设置位数的最佳算法?
Suppose you have a number. Is there any way to count the bits which equals to 1 in binary representation of that number, not using iteration? I mean, is there any way to do it in constant time using some bitwise operators and masks. I need solution which will work well for both architectures 32 bit and 64 bit. Ah almost forgot, I need it for C language or assembler is also good.
假设你有一个号码。有没有办法在这个数字的二进制表示中计算等于1的位,而不是使用迭代?我的意思是,有没有办法使用一些按位运算符和掩码在恒定时间内完成它。我需要的解决方案适用于32位和64位架构。啊差点忘了,我需要它用于C语言或汇编程序也很好。
3 个解决方案
#1
There is a bit counting algorithm without a loop at http://graphics.stanford.edu/~seander/bithacks.html. Lots of bit counting algorithms at http://gurmeetsingh.wordpress.com/2008/08/05/fast-bit-counting-routines/
在http://graphics.stanford.edu/~seander/bithacks.html上有一个没有循环的位计数算法。 http://gurmeetsingh.wordpress.com/2008/08/05/fast-bit-counting-routines/上的大量计数算法
#2
Well, of course there is, but you're not going to like it.
嗯,当然有,但你不会喜欢它。
You could, of course, build a lookup table with all the correct values in it:
当然,您可以构建一个包含所有正确值的查找表:
table[1] = 1, table[2] = 1, table[3] = 2, etc.
表[1] = 1,表[2] = 1,表[3] = 2等
So, this would give you a really fast answer, but it's a completely useless solution by itself, since the table would have to be very, very large.
所以,这会给你一个非常快速的答案,但它本身就是一个完全没用的解决方案,因为这个表必须非常非常大。
You could optimize this a bit, but it requires just a little iteration. Simply create an 8-bit version of the table solution, a mere 256-entry table, then iterate over each BYTE in the value to be checked, summing the results of the table lookup. Something like:
你可以稍微优化一下,但它只需要一点点迭代。只需创建一个8位版本的表解决方案,一个仅仅256个条目的表,然后遍历要检查的值中的每个BYTE,总结表查找的结果。就像是:
short int tableLookup[256] = { 0, 1, 1, 2, 1, ... };
unsigned int valueToCheck = 89392491;
int result = 0;
while ( valueToCheck != 0 ) {
result += tableLookup[ (valueToCheck & 0xFF) ];
valueToCheck >>= 8;
}
// result should now have the correct bit count, if the table is correct.
Hmm, seems this is well known (and here I was doing this off the top of my head): http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetNaive
嗯,似乎这是众所周知的(在这里,我正在做我的头脑):http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetNaive
#1
There is a bit counting algorithm without a loop at http://graphics.stanford.edu/~seander/bithacks.html. Lots of bit counting algorithms at http://gurmeetsingh.wordpress.com/2008/08/05/fast-bit-counting-routines/
在http://graphics.stanford.edu/~seander/bithacks.html上有一个没有循环的位计数算法。 http://gurmeetsingh.wordpress.com/2008/08/05/fast-bit-counting-routines/上的大量计数算法
#2
Well, of course there is, but you're not going to like it.
嗯,当然有,但你不会喜欢它。
You could, of course, build a lookup table with all the correct values in it:
当然,您可以构建一个包含所有正确值的查找表:
table[1] = 1, table[2] = 1, table[3] = 2, etc.
表[1] = 1,表[2] = 1,表[3] = 2等
So, this would give you a really fast answer, but it's a completely useless solution by itself, since the table would have to be very, very large.
所以,这会给你一个非常快速的答案,但它本身就是一个完全没用的解决方案,因为这个表必须非常非常大。
You could optimize this a bit, but it requires just a little iteration. Simply create an 8-bit version of the table solution, a mere 256-entry table, then iterate over each BYTE in the value to be checked, summing the results of the table lookup. Something like:
你可以稍微优化一下,但它只需要一点点迭代。只需创建一个8位版本的表解决方案,一个仅仅256个条目的表,然后遍历要检查的值中的每个BYTE,总结表查找的结果。就像是:
short int tableLookup[256] = { 0, 1, 1, 2, 1, ... };
unsigned int valueToCheck = 89392491;
int result = 0;
while ( valueToCheck != 0 ) {
result += tableLookup[ (valueToCheck & 0xFF) ];
valueToCheck >>= 8;
}
// result should now have the correct bit count, if the table is correct.
Hmm, seems this is well known (and here I was doing this off the top of my head): http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetNaive
嗯,似乎这是众所周知的(在这里,我正在做我的头脑):http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetNaive
#3
Yes, you can do that by using a look up table.
是的,您可以使用查找表来完成此操作。