0 is not a power of 2

Steven Schveighoffer via Digitalmars-d digitalmars-d at puremagic.com
Tue May 19 06:07:43 PDT 2015


On 5/19/15 8:46 AM, Steven Schveighoffer wrote:
> On 5/19/15 1:16 AM, Andrei Alexandrescu wrote:
>> So there's this classic trick:
>>
>> bool isPowerOf2(uint x)
>> {
>>      return (x & (x - 1)) == 0;
>> }
>>
>> Pretty neat, but it wrongly returns true for x == 0. So the obvious
>> fix is:
>>
>> bool isPowerOf2(uint x)
>> {
>>      return x && (x & (x - 1)) == 0;
>> }
>>
>> But that has branches in it. So I came up with:
>>
>> bool isPowerOf2(uint x)
>> {
>>      return (x & (x - 1) | !x) == 0;
>> }
>>
>> which has no branches at least with dmd, see http://goo.gl/TVkCwc.
>
> Is it just me, or is it odd that your first version generates xor and a
> bunch of jumps?
>
> I don't see xor anywhere in the "fast" version.

Nevermind, xor is just zeroing a register.

I will note though, changing the slow version to:

if(x) return (x & (x-1)) == 0;
return 0;

this reduces the jumps by 2.

-Steve


More information about the Digitalmars-d mailing list