Date: Sat, 21 Aug 1999 19:11:35 +0200 From: Johan Karlsson <k@numeri.campus.luth.se> To: Nick Hibma <hibma@skylink.it> Cc: FreeBSD Hackers mailing list <hackers@FreeBSD.ORG> Subject: Re: from number to power of two Message-ID: <199908211711.TAA76885@numeri.campus.luth.se> In-Reply-To: Your message of "Sat, 21 Aug 1999 12:54:32 %2B0200." <Pine.BSF.4.10.9908211250310.7595-100000@heidi.plazza.it>
next in thread | previous in thread | raw e-mail | index | archive | help
At Sat, 21 Aug 1999 12:54:32 +0200, Nick Hibma 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.
>
>The only solution I have so far is a table. That is a possibility as the
>the highest number will be 32 I think.
This small prog works at least on x86
=====
#include <sys/types.h>
#include <machine/cpufunc.h>
int 
main(int argc, char **argv){
  int i, j, k;
  sscanf(argv[1], "%d", &i);
  j = 1<<(fls(i)-1);
  k = 1<<(fls(i-1));
  printf("%d %d %d\n", j, i, k);
  return 0;
} 
=====
/Johan K
To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-hackers" in the body of the message
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199908211711.TAA76885>
