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