上下舍入至2的幂次

      4 int clp2(unsigned int n)                                                                                    
      5 {
      6     --n;
      7     n = n | (n >> 1);
      8     n = n | (n >> 2);
      9     n = n | (n >> 4);
     10     n = n | (n >> 8);
     11     n = n | (n >> 16);
     12     return n + 1;
     13 }

 

unsigned flp2 (unsigned x)

{

  x = x | (x >> 1)

       x = x | (x >> 2)

       x = x | (x >> 4);

       x = x | (x >> 8);

       x = x | (x >> 16);

       return x - (x >> 1);

}

posted on 2012-09-16 19:57  阿加  阅读(206)  评论(0编辑  收藏  举报

导航