Re: x >> x

From: James Turner (turnerjh@XTN.NET)
Date: 07/24/98


I Chose No Name <MUDaholic@AOL.COM> writes:

> okay, i just need a lil help here :)
> i need to convert a (1 >> 7) [which is 128] into
> 7...but i have like a variable 128 that i got from
> 1 >> 7, and i need to convert it back into 7...any help?

Assuming you have a number of the form (1 << n):

int
first_bit(unsigned int v)
{
  int i;

  for (i = 0; i < 32; i++)
    if (v & (1 << i)) return i;

  return -1;
}

Basically what you're wanting to do is take the base 2 logarithm of a
number of the form 2^n.  The above is the easiest way, bit if you have
something like (1 << 4) | (1 << 2), then it will return 2.

--
James Turner                turnerjh@pattern.net         UIN: 1102038
                            http://www.vuse.vanderbilt.edu/~turnerjh/


     +------------------------------------------------------------+
     | Ensure that you have read the CircleMUD Mailing List FAQ:  |
     | http://democracy.queensu.ca/~fletcher/Circle/list-faq.html |
     +------------------------------------------------------------+



This archive was generated by hypermail 2b30 : 12/15/00 PST