Keyboard firmwares for Atmel AVR and Cortex-M
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

util.c 354B

1234567891011121314151617181920
  1. #include "util.h"
  2. // bit population
  3. int bitpop(uint8_t bits)
  4. {
  5. int c;
  6. for (c = 0; bits; c++)
  7. bits &= bits -1;
  8. return c;
  9. }
  10. // most significant on-bit
  11. int biton(uint8_t bits)
  12. {
  13. int n = 0;
  14. if (bits >> 4) { bits >>= 4; n += 4;}
  15. if (bits >> 2) { bits >>= 2; n += 2;}
  16. if (bits >> 1) { bits >>= 1; n += 1;}
  17. return n;
  18. }