Recent Intel/AMD CPUs have a POPCNT instruction, which seems like the logical way to do this. Would be interested to see how that performs compared to these implementations.
FWIW, in gcc/g++ there are compiler intrinsics which (should) map to that instruction on CPUs where it's available: __builtin_popcnt, __builtin_popcountl and __builtin_popcountll for unsigned ints, unsigned longs and unsigned long longs respectively. Visual C++ provides equivalent functions for Windows (but I can't remember what they're called). It does seem odd that the article misses this approach out.
Ten Ways to Check if an Integer Is a Power Of Two in C
31–40 of 82 posts
Re: Ten Ways to Check if an Integer Is a Power Of Two in C
#32Is there a reason this won't work? It's the most 'readable' way I could come up with. #(python code) def is_power_of_two(n): import math if n
Re: Ten Ways to Check if an Integer Is a Power Of Two in C
#33Is there a reason this won't work? It's the most 'readable' way I could come up with. #(python code) def is_power_of_two(n): import math if n
Re: Ten Ways to Check if an Integer Is a Power Of Two in C
#34It seems bizarre to call the first group of methods "decimal based" when the methods don't ever look at the decimal digits of the number being tested. I would call them "arithmetic" or something like that.
It's decimal as in base 10, as opposed to binary.
Re: Ten Ways to Check if an Integer Is a Power Of Two in C
#35Recent Intel/AMD CPUs have a POPCNT instruction, which seems like the logical way to do this. Would be interested to see how that performs compared to these implementations.
Re: Ten Ways to Check if an Integer Is a Power Of Two in C
#36return _mm_popcnt_u64(x) < 2;
Re: Ten Ways to Check if an Integer Is a Power Of Two in C
#37Earlier quoted context omitted.
It's decimal as in base 10, as opposed to binary.
But they aren't in base 10. The constant mentioned might as well have been put into the code in hex or octal and nothing would have changed. (I don't know whether C support binary constants.)
Re: Ten Ways to Check if an Integer Is a Power Of Two in C
#38 int bits(unsigned n)
{
int i = 0;
while (n > 0) { n &= n-1; i++ }
return i;
}Re: Ten Ways to Check if an Integer Is a Power Of Two in C
#39Is there a reason this won't work? It's the most 'readable' way I could come up with. #(python code) def is_power_of_two(n): import math if n
math.log uses floating point arithmetic. That will lead to trouble on large numbers. The article addresses the issue.
>>> is_power_of_two(2 ** 31 - 1)
False
>>> is_power_of_two(2 ** 31)
True
>>> is_power_of_two(2 ** 31 + 1)
False
>>> is_power_of_two(2 ** 548 - 1)
False
>>> is_power_of_two(2 ** 548)
True
>>> is_power_of_two(2 ** 548 + 1)
FalseRe: Ten Ways to Check if an Integer Is a Power Of Two in C
#40Earlier quoted context omitted.
2^-infinity == 0
And 3 == 2^(log_2(3)). Clearly we want to know if x is an integer power of 2.
Edit: Yes, 1 << bitsize is undefined. But unsigned integers actually do have well-defined semantics on overflow, and multiplying by 2 enough times really does produce zero.