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