求1的个数

来源:互联网 发布:wps mac 编辑:程序博客网 时间:2024/05/21 05:08
  1. package BitCount;  
  2.   
  3. /** 
  4.  * 任意给定一个32位无符号整数n,求n的二进制表示中1的个数,比如n = 5(0101)时,返回2,n = 15(1111)时,返回4 
  5.  *  
  6.  * @author vivizhyy 
  7.  *  
  8.  */  
  9. public interface BitCountMethods {  
  10.   
  11.     /** 移位+计数 */  
  12.     public int normal(int x);  
  13.   
  14.     /** 不断清除x的二进制表示中最右边的1,同时累加计数器,直至x为0 */  
  15.     public int quick(int x);  
  16.   
  17.     /** 
  18.      * @see #static8bit(int) 
  19.      */  
  20.     public int static4bit(int x);  
  21.   
  22.     /** 
  23.      * 首先构造一个包含256个元素的表table,table[i]即i中1的个数,这里的i是[0-255]之间任意一个值。 
  24.      * 然后对于任意一个32bit无符号整数n 
  25.      * ,我们将其拆分成四个8bit,然后分别求出每个8bit中1的个数,再累加求和即可,这里用移位的方法,每次右移8位 
  26.      * ,并与0xff相与,取得最低位的8bit 
  27.      * ,累加后继续移位,如此往复,直到n为0。所以对于任意一个32位整数,需要查表4次。以十进制数2882400018为例 
  28.      * ,其对应的二进制数为10101011110011011110111100010010 
  29.      * ,对应的四次查表过程如下:红色表示当前8bit,绿色表示右移后高位补零。 
  30.      *  
  31.      * 第一次(n & 0xff) 10101011110011011110111100010010 
  32.      *  
  33.      * 第二次((n >> 8) & 0xff) 00000000101010111100110111101111 
  34.      *  
  35.      * 第三次((n >> 16) & 0xff)00000000000000001010101111001101 
  36.      *  
  37.      * 第四次((n >> 24) & 0xff)00000000000000000000000010101011 
  38.      */  
  39.     public int static8bit(int x);  
  40.   
  41.     /** 先将n写成二进制形式,然后相邻位相加,重复这个过程,直到只剩下一位。  
  42.      * 1 1  0 1  1 0  0 1 
  43.      * \ /  \ /  \ /  \ / 
  44.      *  2    1    1    1 
  45.      *  \    /     \   / 
  46.      *     3         2 
  47.      *     \        / 
  48.      *          5 
  49.      */  
  50.     public int parallel(int x);  
  51.       
  52.     /** http://www.cnblogs.com/graphics/archive/2010/06/21/1752421.html */  
  53.     public int perfectness(int x);  
  54.   
  55. }  

 

Java代码  收藏代码
  1. package BitCount;  
  2.   
  3. public class BitCounts implements BitCountMethods {  
  4.   
  5.     @Override  
  6.     public int normal(int x) {  
  7.         int c = 0;  
  8.         for (; x > 0; x >>>= 1) {  
  9.             c += x & 1// 如果当前位是 1,计数器加 1  
  10.         }  
  11.         return c;  
  12.     }  
  13.   
  14.     @Override  
  15.     public int quick(int x) {  
  16.         int c = 0;  
  17.         for (; x > 0; c++) {  
  18.             x &= (x - 1); // 清除最右边的 1  
  19.         }  
  20.         return c;  
  21.     }  
  22.   
  23.     @Override  
  24.     public int static4bit(int x) {  
  25.         int[] table = { 0112122312232334 };  
  26.         int c = 0;  
  27.         for (; x > 0; x >>>= 4) {  
  28.             c += table[x & 0xF];  
  29.         }  
  30.   
  31.         return c;  
  32.     }  
  33.   
  34.     @Override  
  35.     public int static8bit(int x) {  
  36.         int[] table = { 011212231223233412,  
  37.                 232334233434451223233,  
  38.                 423343445233434453445,  
  39.                 455612232334233434452,  
  40.                 334344534454556233434,  
  41.                 453445455634454556455,  
  42.                 656671223233423343445,  
  43.                 233434453445455623343,  
  44.                 445344545563445455645,  
  45.                 565667233434453445455,  
  46.                 634454556455656673445,  
  47.                 455645565667455656675,  
  48.                 6676778, };  
  49.         int c = 0;  
  50.         for(; x > 0; x >>>= 8){  
  51.             c += table[x & 0xFF];  
  52.         }  
  53.           
  54.         return c;  
  55.     }  
  56.   
  57.     @Override  
  58.     public int parallel(int x) {  
  59.         // 0x55 = 0101 0101  
  60.         x = (x & 0x55555555) + ((x >>> 1) & 0x55555555);  
  61.         //0x33 = 0011 0011  
  62.         x = (x & 0x33333333) + ((x >>> 2) & 0x33333333);  
  63.         //0x0f = 0000 1111  
  64.         x = (x & 0x0f0f0f0f) + ((x >>> 4) & 0x0f0f0f0f);  
  65.         //0x00ff = 0000 0000 1111 1111  
  66.         x = (x & 0x00ff00ff) + ((x >>> 8) & 0x00ff00ff);  
  67.         //0x0000ffff = 0000 0000 0000 0000 1111 1111 1111 1111  
  68.         x = (x & 0x0000ffff) + ((x >>> 16) & 0x0000ffff);  
  69.           
  70.         return x;  
  71.     }  
  72.   
  73.     @Override  
  74.     public int perfectness(int x) {  
  75.         int temp = x - (x >>> 1) & 033333333333 - (x >>> 2) & 011111111111;  
  76.         return (temp +(temp >>>3)) & 030707070707 % 63;  
  77.     }  
  78.   
  79.   
  80. }  

 

Java代码  收藏代码
  1. package BitCount;  
  2.   
  3. import static org.junit.Assert.*;  
  4.   
  5. import org.junit.Test;  
  6.   
  7. public class BitCountMethodsTest {  
  8.     BitCountMethods bcm = new BitCounts();  
  9.     int x = 123;  
  10.   
  11.     @Test  
  12.     public final void testNormal() {  
  13.         assert(bcm.normal(x) == 6);  
  14.     }  
  15.   
  16.     @Test  
  17.     public final void testQuick() {  
  18.         assert(bcm.quick(x) == 6);  
  19.     }  
  20.   
  21.     @Test  
  22.     public final void testStatic4bit() {  
  23.         assert(bcm.static4bit(x) == 6);  
  24.     }  
  25.   
  26.     @Test  
  27.     public final void testStatic8bit() {  
  28.         assert(bcm.static8bit(x) == 6);  
  29.     }  
  30.   
  31.     @Test  
  32.     public final void testParallel() {  
  33.         assert(bcm.parallel(x) == 6);  
  34.     }  
  35.   
  36.     @Test  
  37.     public final void testPerfectness() {  
  38.         assert(bcm.perfectness(x) == 6);  
  39.     }  
  40.   

0 0