home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / programm / 2037 < prev    next >
Encoding:
Internet Message Format  |  1992-07-21  |  983 b 

  1. Path: sparky!uunet!uunet!not-for-mail
  2. From: avg@rodan.UU.NET (Vadim Antonov)
  3. Newsgroups: comp.programming
  4. Subject: Re: finding 1st one in integer
  5. Date: 21 Jul 1992 16:38:28 -0400
  6. Organization: Berkeley Software Design
  7. Lines: 27
  8. Message-ID: <14hsk4INNke@rodan.UU.NET>
  9. References: <Brqu3F.1J4@undergrad.math.waterloo.edu> <1992Jul21.173805.12045@bcrka451.bnr.ca>
  10. NNTP-Posting-Host: rodan.uu.net
  11.  
  12. inline int highbitpos(register int i)
  13. {
  14.     register int bitpos = 0;
  15.  
  16.     if( i & 0xffff0000 ) {
  17.         i >>= 16;
  18.         bitpos = 16;
  19.     }
  20.     if( i & 0xff00 ) {
  21.         i >>= 8;
  22.         bitpos += 8;
  23.     }
  24.     if( i & 0xf0 ) {
  25.         i >>= 4;
  26.         bitpos += 4;
  27.     }
  28.     bitpos += "\377\0\1\1\2\2\2\2\3\3\3\3\3\3\3\3"[i];
  29.     return bitpos;
  30. }
  31.  
  32. You can speed it up by sacrificing some data space and removing the
  33. last _if_. There are variants which do not requre additional register
  34. variable or addition at the end (the first makes the code bigger, the
  35. second requires replicating the bit tables). All those trade-offs are
  36. up to you :-)
  37.  
  38. --vadim
  39.