home *** CD-ROM | disk | FTP | other *** search
/ Power Programming / powerprogramming1994.iso / progtool / c / string.arc / FFS.C < prev    next >
C/C++ Source or Header  |  1984-12-31  |  1KB  |  44 lines

  1. /*  File   : ffs.c
  2.     Author : Richard A. O'Keefe.
  3.     Updated: 20 April 1984
  4.     Defines: ffs(), ffc()
  5.  
  6.     ffs(i) returns the index of the least significant 1 bit in i,
  7.        where 1 means the least significant bit and 32 means the
  8.        most significant bit, or returns -1 if i is 0.
  9.  
  10.     ffc(i) returns the index of the least significant 0 bit in i,
  11.        where 1 means the least significant bit and 32 means the
  12.        most significant bit, or returns -1 if i is ~0.
  13.  
  14.     These functions mimic the VAX FFS and FFC instructions, except that
  15.     the latter return much more sensible values.  This file only exists
  16.     to make it easier to move 4.2bsd programs to System III (which is
  17.     rather like moving up from a Rolls Royce to a model T Ford), and so
  18.     I haven't bother with assembly code versions.
  19. */
  20.  
  21. #include "strings.h"
  22.  
  23. int ffs(i)
  24.     register int i;
  25.     {
  26.     register int N;
  27.  
  28.     for (N = 8*sizeof(int); --N >= 0; i >>= 1)
  29.         if (i&1) return 8*sizeof(int)-N;
  30.     return -1;
  31.     }
  32.  
  33. int ffc(i)
  34.     register int i;
  35.     {
  36.     register int N;
  37.  
  38.     for (N = 8*sizeof(int); --N >= 0; i >>= 1)
  39.         if (!(i&1)) return 8*sizeof(int)-N;
  40.     return -1;
  41.     }
  42.  
  43.