0 is not a power of 2

Andrei Alexandrescu via Digitalmars-d digitalmars-d at puremagic.com
Wed May 20 08:53:21 PDT 2015


On 5/19/15 1:46 PM, Matthias Bentrup wrote:
> I think you can make the over/underflow at zero work in your favor:
>
> bool isPowerOf2(uint x)
> {
>    return (x & -x) > (x - 1);
> }

Nice code with dmd and gdc. Thanks! 
https://github.com/andralex/phobos/commit/ec197ecd203b0ea25201acfeb4fbbb13b2fabb7f 
-- Andrei


More information about the Digitalmars-d mailing list