From owner-freebsd-hackers Sun Aug 22 0:50:38 1999 Delivered-To: freebsd-hackers@freebsd.org Received: from gratis.grondar.za (gratis.grondar.za [196.7.18.133]) by hub.freebsd.org (Postfix) with ESMTP id 3C51714E22 for ; Sun, 22 Aug 1999 00:50:27 -0700 (PDT) (envelope-from mark@grondar.za) Received: from grondar.za (localhost [127.0.0.1]) by gratis.grondar.za (8.9.3/8.9.3) with ESMTP id JAA69064; Sun, 22 Aug 1999 09:50:13 +0200 (SAST) (envelope-from mark@grondar.za) Message-Id: <199908220750.JAA69064@gratis.grondar.za> To: Nick Hibma Cc: FreeBSD Hackers mailing list Subject: Re: from number to power of two Date: Sun, 22 Aug 1999 09:50:12 +0200 From: Mark Murray Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG > 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 To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-hackers" in the body of the message