程序中使用的逻辑是什么?

时间:2014-04-04 10:28:19

标签: c algorithm

代码来自链接C puzzles

int CountBits (unsigned int x )
{
      static unsigned int mask[] = { 0x55555555,
          0x33333333,
          0x0F0F0F0F,
          0x00FF00FF,
          0x0000FFFF
          } ;

          int i ;
          int shift ; /* Number of positions to shift to right*/
          for ( i =0, shift =1; i < 5; i ++, shift *= 2)
                  x = (x & mask[i ])+ ( ( x >> shift) & mask[i]);
          return x;
}

0 个答案:

没有答案