lkml.org 
[lkml]   [2007]   [Jun]   [15]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
Date
From
SubjectRe: [PATCH] Optimize is_power_of_2().
On Fri, 15 Jun 2007 21:47:50 +0200 (CEST)
Jan Engelhardt <jengelh@computergmbh.de> wrote:

> On Jun 15 2007 18:56, Vegard Nossum wrote:
> > bool is_power_of_2(unsigned long n)
> > {
> >- return (n != 0 && ((n & (n - 1)) == 0));
> >+ return n * !(n & (n - 1));
> > }
>
> There is a third way which uses neither * nor &&, but []:

I assume using something GCC-specific is right out?

bool is_power_of_to(unsigned long n)
{
return __builtin_ffsl(n) == 1;
}
- DML
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

\
 
 \ /
  Last update: 2007-06-15 21:57    [from the cache]
©2003-2011 Jasper Spaans