From owner-freebsd-hackers Sun Aug 22 1:13: 4 1999 Delivered-To: freebsd-hackers@freebsd.org Received: from ns.skylink.it (ns.skylink.it [194.177.113.1]) by hub.freebsd.org (Postfix) with ESMTP id 5D4D915009 for ; Sun, 22 Aug 1999 01:13:01 -0700 (PDT) (envelope-from n_hibma@skylink.it) Received: from heidi.plazza.it (va-156.skylink.it [194.185.55.156]) by ns.skylink.it (8.9.1/8.8.8) with ESMTP id KAA07903; Sun, 22 Aug 1999 10:13:02 +0200 Received: from localhost (localhost [127.0.0.1]) by heidi.plazza.it (8.9.3/8.8.5) with ESMTP id KAA20938; Sun, 22 Aug 1999 10:00:35 +0200 (CEST) Date: Sun, 22 Aug 1999 10:00:35 +0200 (CEST) From: Nick Hibma X-Sender: n_hibma@heidi.plazza.it Reply-To: Nick Hibma To: Mark Murray Cc: FreeBSD Hackers mailing list Subject: Re: from number to power of two In-Reply-To: <199908220750.JAA69064@gratis.grondar.za> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG It seems that all the solutions are too generic and slow. As I only have to check the numbers 0-32 (actually 1-32), a block of if statements is almost as fast as a table look up in 33 elements. Cheers, Nick On Sun, 22 Aug 1999, Mark Murray wrote: > > Does anyone know an inexpensive algorithm (O(1)) to go from an number to > > the next (lower or higher) power of two. > > > > 1 -> 1 > > 2,3 -> 2 > > 4,5,6,7 -> 4 > > 8,9,10,11,12,13,14,15 -> 8 > > etc. > > > > So %1101 should become either %10000 or %1000. > > Shift a bit until it becomes greater than (or less than) the number > in question. > > M > -- > Mark Murray > Join the anti-SPAM movement: http://www.cauce.org > > -- e-Mail: hibma@skylink.it To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-hackers" in the body of the message