Checking if an Integer is an Exact Binary Power

Dmitry Olshansky via Digitalmars-d digitalmars-d at puremagic.com
Sat Apr 23 07:41:09 PDT 2016


On 23-Apr-2016 16:04, Nordlöw wrote:
> Wanted: CT-trait and run-time predicate for checking whether its single
> integer parameter is an exact power of two.
>
> I guess
>
> https://dlang.org/phobos/std_math.html#.truncPow2
>
> or
>
> https://dlang.org/phobos/std_math.html#.nextPow2
>
> could be reused, but I bet there's a more efficient way of checking this.

x & (x-1) == 0

-- 
Dmitry Olshansky


More information about the Digitalmars-d mailing list