[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <Pine.LNX.4.64.0706152140171.32724@fbirervta.pbzchgretzou.qr>
Date: Fri, 15 Jun 2007 21:47:50 +0200 (CEST)
From: Jan Engelhardt <jengelh@...putergmbh.de>
To: Vegard Nossum <vegard.nossum@...il.com>
cc: Linux Kernel Mailing List <linux-kernel@...r.kernel.org>,
"H. Peter Anvin" <hpa@...or.com>,
"Robert P. J. Day" <rpjday@...dspring.com>
Subject: Re: [PATCH] Optimize is_power_of_2().
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 []:
bool is_power_of_2(unsigned long n)
{
static const bool yn[] = {
[0] = false,
[1 ... (8 * sizeof(n) - 1)] = true,
};
return yn[(n & (n - 1)) == 0];
}
Jan
--
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@...r.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
Powered by blists - more mailing lists