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

  1. Newsgroups: comp.programming
  2. Path: sparky!uunet!munnari.oz.au!bunyip.cc.uq.oz.au!marlin.jcu.edu.au!coral.cs.jcu.edu.au!spuler
  3. From: spuler@coral.cs.jcu.edu.au (David Spuler)
  4. Subject: Re: Finding the first bit set
  5. Message-ID: <spuler.711869097@coral.cs.jcu.edu.au>
  6. Sender: news@marlin.jcu.edu.au (USENET News System)
  7. Organization: James Cook University
  8. References: <1992Jul22.160034.7162@uwm.edu>
  9. Date: 23 Jul 92 05:24:57 GMT
  10. Lines: 15
  11.  
  12. In <1992Jul22.160034.7162@uwm.edu> markh@csd4.csd.uwm.edu (Mark William Hopkins) writes:
  13.  
  14. How about
  15.  
  16. int FirstBit(unsigned long L) {
  17.     return L - (L & (L-1));
  18. }
  19.  
  20. since L&(L-1) removes the first bit (try it!) then L - (L&(L-1) or L^(L&(L-1)
  21. should yield only that bit. It's so neat it could be made a macro.
  22. --
  23. David Spuler, James Cook University of North Queensland, Australia
  24. Author of "Comprehensive C", Prentice-Hall, 1992, pp416, ISBN  0-13-156514-1
  25. INTRO TOPICS: types, operators, structures, strings, fns, ptrs, files etc etc
  26. ADVANCED TOPICS: efficiency, debugging, style, portability, large programs
  27.