Checking if an Integer is an Exact Binary Power

safety0ff via Digitalmars-d digitalmars-d at puremagic.com
Sat Apr 23 14:58:44 PDT 2016


On Saturday, 23 April 2016 at 21:04:52 UTC, Nordlöw wrote:
> On Saturday, 23 April 2016 at 20:42:25 UTC, Lass Safin wrote:
>> CPUID: https://en.wikipedia.org/wiki/CPUID.
>> You can check for the presence of a lot of instructions with 
>> this instruction.
>> However this will only work on x86 and only run-time.
>
> Code you give a complete code example in D, please or point out 
> a suitable place in druntime/phobos?

https://dlang.org/phobos/core_cpuid.html#.hasPopcnt

However, it is usually better to use the methods stated by Andrei 
/ Dmitry* rather than population count for checking powers of two.

* With the code given by Dmitry you have to check for zero, 
otherwise it will return true for 0. e.g.  x && !(x & (x - 1))


More information about the Digitalmars-d mailing list