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

  1. Newsgroups: comp.programming
  2. Path: sparky!uunet!europa.asd.contel.com!darwin.sura.net!convex!news.utdallas.edu!corpgate!bnrgate!bcrka451!bcrki65!sjm
  3. From: sjm@bcrki65.bnr.ca (Stuart MacMartin)
  4. Subject: Re: finding 1st one in integer
  5. Message-ID: <1992Jul21.173805.12045@bcrka451.bnr.ca>
  6. Sender: 5E00 Corkstown News Server
  7. Reply-To: sjm@bcrki65.bnr.ca (Stuart MacMartin)
  8. Organization: Bell-Northern Research, Ottawa, Canada
  9. References: <Brqu3F.1J4@undergrad.math.waterloo.edu> 
  10. Date: Tue, 21 Jul 1992 17:38:05 GMT
  11. Lines: 36
  12.  
  13. In article <Brqu3F.1J4@undergrad.math.waterloo.edu>,
  14. amichail@cayley.waterloo.edu (Amir Michail) writes:
  15. > I need a very efficient way of finding the first bit set ( doesn't matter
  16. > which side ) in a 32 bit integer. 
  17. > For example:
  18. >   binary  -> position
  19. >       1 -> 0
  20. >     10000 -> 4
  21. >      1000 -> 3
  22. > One can make the simplifying assumption that only 1 bit will be
  23. > set in the 32 bit integer (if that helps).
  24. > Amir
  25. > P.S.  Please do not tell me to use a large lookup table.  I know about that.
  26. >       I also know that the 68020 and 80386 have instructions to do
  27. this; I want
  28. >       portable C code though.
  29.  
  30. Check a byte at a time for non-zero (being careful not to assume byte
  31. ordering or behaviour of shifting signed values if you want to be portable).
  32.  
  33. Either use a lookup table of 256 values, or split the byte into two
  34. so that your table is only 16 long.
  35.  
  36. If you often have numbers with the 2 lowest bytes clear, you might want to
  37. start by checking 2-byte values against 0.
  38.  
  39. Stuart
  40.  
  41. : Stuart MacMartin                                    email: sjm@bnr.ca      :
  42. : Bell-Northern Research                              phone: (613) 763-5625  :
  43. : PO Box 3511, Stn C, Ottawa, K1Y-4H7, CANADA    Standard disclaimers apply. :
  44.