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

  1. Newsgroups: comp.programming
  2. Path: sparky!uunet!zaphod.mps.ohio-state.edu!sol.ctr.columbia.edu!destroyer!fmsrl7!wdl1!wdl39!mab
  3. From: mab@wdl39.wdl.loral.com (Mark A Biggar)
  4. Subject: Re: finding 1st one in integer
  5. Message-ID: <1992Jul21.205127.23216@wdl.loral.com>
  6. Sender: news@wdl.loral.com
  7. Organization: Loral Western Development Labs
  8. References: <Brqu3F.1J4@undergrad.math.waterloo.edu> <1992Jul21.173805.12045@bcrka451.bnr.ca> <1992Jul21.150033@is.morgan.com>
  9. Date: Tue, 21 Jul 1992 20:51:27 GMT
  10. Lines: 24
  11.  
  12. In article <1992Jul21.150033@is.morgan.com> berlin@is.morgan.com (Alexander Berlin) writes:
  13. >how about this one line solution:
  14. >
  15. >unsigned int i;  /* given integer */
  16. >unsigned int j;  /* working variable */
  17. >int          bitset;  /* result */
  18. >
  19. >   for(j=1, bitset=1; ; bitset++, j+=j) if (i==j) break; /* sets bitset */
  20. >
  21. >Will work, of course, only when exactly one bit is set. But you want
  22. >efficiency, so there is no unnecessary conditions. And it looks portable -
  23. >doesn't even rely on the int size. 
  24.  
  25. This is no problem as i^(i-1) produces a value consisting on just the least
  26. sigfinicant bit in i
  27.  
  28. Athough your solution is really no faster then right shifting 1 bit at a time
  29. until odd and counting the shifts.  For 32 numbers or larger table lookup
  30. solutions will typically be faster. 
  31. --
  32. Mark Biggar
  33. mab@wdl1.loral.com
  34.  
  35.  
  36.