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