home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / programm / 2041 < prev    next >
Encoding:
Text File  |  1992-07-21  |  1.4 KB  |  47 lines

  1. Newsgroups: comp.programming
  2. Path: sparky!uunet!s5!is1.is.morgan.com!is.morgan.com!berlin
  3. From: berlin@is.morgan.com (Alexander Berlin)
  4. Subject: Re: finding 1st one in integer
  5. Message-ID: <1992Jul21.165726@is.morgan.com>
  6. Sender: news@is.morgan.com
  7. Nntp-Posting-Host: chico
  8. Organization: Morgan Stanley - IS
  9. References: <Brqu3F.1J4@undergrad.math.waterloo.edu> <1992Jul21.173805.12045@bcrka451.bnr.ca> <14hsk4INNke@rodan.UU.NET>
  10. Date: Tue, 21 Jul 1992 20:57:26 GMT
  11. Lines: 34
  12.  
  13. In article <14hsk4INNke@rodan.UU.NET>, avg@rodan.UU.NET (Vadim Antonov) writes:
  14. |> inline int highbitpos(register int i)
  15. |> {
  16. |>     register int bitpos = 0;
  17. |> 
  18. |>     if( i & 0xffff0000 ) {
  19. |>         i >>= 16;
  20. |>         bitpos = 16;
  21. |>     }
  22. |>     if( i & 0xff00 ) {
  23. |>         i >>= 8;
  24. |>         bitpos += 8;
  25. |>     }
  26. |>     if( i & 0xf0 ) {
  27. |>         i >>= 4;
  28. |>         bitpos += 4;
  29. |>     }
  30. |>     bitpos += "\377\0\1\1\2\2\2\2\3\3\3\3\3\3\3\3"[i];
  31. |>     return bitpos;
  32. |> }
  33. |> 
  34. |> You can speed it up by sacrificing some data space and removing the
  35. |> last _if_. There are variants which do not requre additional register
  36. |> variable or addition at the end (the first makes the code bigger, the
  37. |> second requires replicating the bit tables). All those trade-offs are
  38. |> up to you :-)
  39. |> 
  40. |> --vadim
  41.  
  42. Vadim, you are assuming Big Endian, aren't you?
  43. The original article asked for PORTABLE code. ">>=" depends on byte order.
  44. ---
  45. Alex Berlin                     
  46. berlin@is.morgan.com
  47.