Nick Hibma scribbled this message on Aug 21:
> 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.

hmmm....  how about:
int
fls(int n)
{
        /* courtesy of fortune, of course this is O(lgn) */
        /* swap the bits around */
        n = ((n >>  1) & 0x55555555) | ((n <<  1) & 0xaaaaaaaa);
        n = ((n >>  2) & 0x33333333) | ((n <<  2) & 0xcccccccc);
        n = ((n >>  4) & 0x0f0f0f0f) | ((n <<  4) & 0xf0f0f0f0);
        n = ((n >>  8) & 0x00ff00ff) | ((n <<  8) & 0xff00ff00);
        n = ((n >> 16) & 0x0000ffff) | ((n << 16) & 0xffff0000);

        /* mask all but the lowest bit off */
        n = n & -n;

        /* swap them back to where they were originally */
        n = ((n >>  1) & 0x55555555) | ((n <<  1) & 0xaaaaaaaa);
        n = ((n >>  2) & 0x33333333) | ((n <<  2) & 0xcccccccc);
        n = ((n >>  4) & 0x0f0f0f0f) | ((n <<  4) & 0xf0f0f0f0);
        n = ((n >>  8) & 0x00ff00ff) | ((n <<  8) & 0xff00ff00);
        n = ((n >> 16) & 0x0000ffff) | ((n << 16) & 0xffff0000);

        return n;
}

now this is O(lgn) (where n is number of bits in an int), but I'm not
sure how this will perform wrt the other posting.. it probably won't be
that fast because of all the arithmetic that happens, and a binary search
of the number would probably be faster (which I have implemented a few
times)...

-- 
  John-Mark Gurney                              Voice: +1 541 684 8449
  Cu Networking                                   P.O. Box 5693, 97405

  "The soul contains in itself the event that shall presently befall it.
  The event is only the actualizing of its thought." -- Ralph Waldo Emerson


To Unsubscribe: send mail to [EMAIL PROTECTED]
with "unsubscribe freebsd-hackers" in the body of the message

Reply via email to