Checking if an Integer is an Exact Binary Power

David Nadlinger via Digitalmars-d digitalmars-d at puremagic.com
Sun Apr 24 16:17:53 PDT 2016


On Sunday, 24 April 2016 at 23:00:56 UTC, Temtaime wrote:
> Please no cmp.
> Just
> bool isPowerOf2(uint x) { return x && !(x & (x - 1)); }

You do realise that this will (typically) emit a branch?

  — David


More information about the Digitalmars-d mailing list