0 is not a power of 2

Dominikus Dittes Scherkl via Digitalmars-d digitalmars-d at puremagic.com
Thu May 21 07:53:01 PDT 2015


On Tuesday, 19 May 2015 at 20:46:09 UTC, 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);
> }

The cool thing is that also the over/underflow of "-" at 
0x8000_0000 works. But that is really a weird calculation! 
Wouldn't pass any Polyspace or other code checker tool and need 
some special comments on why it works...


More information about the Digitalmars-d mailing list