home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / programm / 2058 < prev    next >
Encoding:
Text File  |  1992-07-22  |  808 b   |  25 lines

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