home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.programming
- Path: sparky!uunet!cis.ohio-state.edu!pacific.mps.ohio-state.edu!linac!uwm.edu!csd4.csd.uwm.edu!markh
- From: markh@csd4.csd.uwm.edu (Mark William Hopkins)
- Subject: Finding the first bit set
- Message-ID: <1992Jul22.160034.7162@uwm.edu>
- Sender: news@uwm.edu (USENET News System)
- Organization: Computing Services Division, University of Wisconsin - Milwaukee
- Date: Wed, 22 Jul 1992 16:00:34 GMT
- Lines: 14
-
- For the case of 32 bit words, most compilers should handle this efficiently
- for any 32-bit architecture.
-
- #include <stdio.h>
- int FirstBit(unsigned long L) {
- int I = 0;
- if (!(L&0xffff)) L >>= 16, I |= 16;
- if (!(L&0xff)) L >>= 8, I |= 8;
- if (!(L&0xf)) L >>= 4, I |= 4;
- if (!(L&0x3)) L >>= 2, I |= 2;
- if (!(L&0x1)) L >>= 1, I |= 1;
- if (!L) I++;
- return I;
- }
-