ffs.c 465 B

123456789101112131415161718192021222324252627
  1. /* ffs -- Find the first bit set in the parameter
  2. @deftypefn Supplemental int ffs (int @var{valu})
  3. Find the first (least significant) bit set in @var{valu}. Bits are
  4. numbered from right to left, starting with bit 1 (corresponding to the
  5. value 1). If @var{valu} is zero, zero is returned.
  6. @end deftypefn
  7. */
  8. int
  9. ffs (register int valu)
  10. {
  11. register int bit;
  12. if (valu == 0)
  13. return 0;
  14. for (bit = 1; !(valu & 1); bit++)
  15. valu >>= 1;
  16. return bit;
  17. }