1. 程式人生 > >如何計算一個位元組中1的位數

如何計算一個位元組中1的位數

一個巧妙的演算法

static  uint8_t bitcount(uint8_t n)  
{
  uint8_t count=0 ;
  while (n)  
  {
      count++ ;
      n &= (n - 1) ;
  }
  return count ;
}