home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.programming
- Path: sparky!uunet!tarpit!bilver!alex
- From: alex@bilver.uucp (Alex Matulich)
- Subject: Re: finding 1st one in integer
- Organization: W. J. Vermillion - Winter Park, FL
- Date: Fri, 24 Jul 1992 03:01:58 GMT
- Message-ID: <1992Jul24.030158.15492@bilver.uucp>
- References: <Brqu3F.1J4@undergrad.math.waterloo.edu>
- Lines: 24
-
- 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.
-
- How about this one? It finds the position of the high-order bit, and it
- works even if more than 1 bit is set.
-
- int firstbit(int n)
- {
- int i = 0, j;
- while ( (j = n << 1) > n) {
- ++i;
- n = j;
- }
- return i;
- }
-
- This function will return a number that assumes bit 0 is the high-order bit.
-
- --
- _ |__ Alex Matulich
- /(+__> Unicorn Research Corp, 4621 N Landmark Dr, Orlando, FL 32817
- //| \ UUCP: alex@bilver.uucp {peora|ge-dab|uunet!tous}!bilver!alex
- ///__) Internet: alex@bilver.oau.org | alex%bilver@peora.sdc.ccur.com
-