• 32位无符号整数中1的个数


          4 int bitcount(unsigned int n)
          5 {
          6     n = n - ((n>>1)&033333333333) - ((n>>2)&011111111111);
          7     n = (n + (n>>3))&030707070707;

          8     n = n % 0x3f;
          9     return n;
          10 }

       17 int bitcount(unsigned int n)
         18 {
         19     n = n - ((n>>1)&0x77777777) - ((n>>2)&0x33333333)-((n>>3)&0x11111111);
         20     n = (n + (n>>4))&0xf0f0f0f;

         21     n = n + ((n >> 8) & 0xf) + ((n >> 16) & 0xf) + ((n >> 24) & 0xf);
         22     n = n & 0xff;
         23     return n;                                                                                             
         24 }

         4 int bitcount(unsigned int n)
          5 {
          6     unsigned int tmp;
          7     tmp = n & 0x33333333;
          8     n = n - tmp;
          9     n = (n >> 2) & 0x33333333;
         10     tmp = tmp - ((tmp >> 1) & 0x33333333);
         11     n = n - ((n >> 1) & 0x33333333);
         12     tmp = tmp + n;
         13     n = tmp + (tmp >> 16);
         14     tmp = (n & 0xf) + ((n >> 4) & 0xf) +
         15           ((n >> 8) & 0xf) + ((n >> 12) & 0xf);
         16     tmp = tmp & 0xff;
         17     return tmp;
         18 }

        int bitcount(unsigned int n)

    {

      unsigned x;

          x = (n >> 1) & 0x77777777;

          n = n - x;

          x = (x >> 1) & 0x77777777;

          n = n - x;

          x = (x >> 1) & 0x77777777;

          n = n -x;

          x = (n + (n >> 16));

          n =  (n & 0xf) + ((n >> 4) & 0xf) + ((n >> 8) & 0xf) + ((n >> 12) & 0xf);

           x = n & 0xff;

          return x;

    }

  • 相关阅读:
    算法-对分查找(二分查找)C++实现
    Android Studio简单设置
    一起talk C栗子吧(第八回:C语言实例--素数)
    Maven生命周期
    java8_api_日期时间
    UITableviewcell的性能问题
    iOS开发25个性能调优技巧
    iOS9新特性
    iOS9 3DTouch、ShortcutItem、Peek And Pop技术一览
    iOS网络访问之使用AFNetworking
  • 原文地址:https://www.cnblogs.com/openix/p/2687774.html
Copyright © 2020-2023  润新知