variable precision SWAR算法
二进制中1的个数
http://www.nowcoder.com/questionTerminal/8ee967e43c2c4ec193b040ea7fbb10b8
经典Redis中BITCOUNT命令的算法
原理可以看这篇博客
public class JZ11 { public int NumberOf1(int n) { n = (n & 0x55555555) + ((n >> 1) & 0x55555555); n = (n & 0x33333333) + ((n >> 2) & 0x33333333); n = (n & 0x0F0F0F0F) + ((n >> 4) & 0x0F0F0F0F); n = ((n * 0x01010101) >> 24); return n; } @Test public void Test() { Assert.assertEquals(NumberOf1(10), 2); Assert.assertEquals(NumberOf1(0),0); Assert.assertEquals(NumberOf1(Integer.MAX_VALUE), 31); Assert.assertEquals(NumberOf1(Integer.MIN_VALUE), 1); Assert.assertEquals(NumberOf1(-1),32); } }