- Hamming weight
The Hamming weight of a string is the number of symbols that are different from the zero-symbol of the
alphabet used.It is thus equivalent to the
Hamming distance from the all-zero string of the same length. For the most typical case, a string ofbit s, this is the number of 1's in the string. In this binary case, it is also called the population count, or popcount.Examples
Subtracting 1 changes the rightmost string of 0s to 1s, and changes the rightmost 1 to a 0. The AND then removes that rightmost 1. If X originally had N bits that were 1, then after only N iterations of this operation, X will be reduced to zero. The following are based on this principle.
//This is better when most bits in x are 0//It uses 3 arithmetic operations and one comparison/branch per "1" bit in x.int popcount_4(uint64 x) { uint64 count; for (count=0; x; count++) x &= x-1; return count;} //This is better if most bits in x are 0.//It uses 2 arithmetic operations and one comparison/branch per "1" bit in x.//It is the same as the previous function, but with the loop unrolled.
#define f(y) if ((x &= x-1) = 0) return y;int popcount_5(uint64 x) { if (x = 0) return 0; f( 1) f( 2) f( 3) f( 4) f( 5) f( 6) f( 7) f( 8) f( 9) f(10) f(11) f(12) f(13) f(14) f(15) f(16) f(17) f(18) f(19) f(20) f(21) f(22) f(23) f(24) f(25) f(26) f(27) f(28) f(29) f(30) f(31) f(32) f(33) f(34) f(35) f(36) f(37) f(38) f(39) f(40) f(41) f(42) f(43) f(44) f(45) f(46) f(47) f(48) f(49) f(50) f(51) f(52) f(53) f(54) f(55) f(56) f(57) f(58) f(59) f(60) f(61) f(62) f(63) return 64;}//Use this instead if most bits in x are 1 instead of 0
#define f(y) if ((x |= x+1) = hff) return 64-y;Language Support
In
C++ STL , the bit-array data structurebitset
has acount()
method that counts the number of bits that are set.In Java, the growable bit-array data structure Javadoc:SE|java/util|BitSet has a Javadoc:SE|java/util|BitSet|cardinality() method that counts the number of bits that are set. In addition, there are Javadoc:SE|java/lang|Integer|bitCount(int) and Javadoc:SE|java/lang|Long|bitCount(long) functions to count bits in primitive 32-bit and 64-bit integers, respectively. Also, the Javadoc:SE|java/math|BigInteger arbitrary-precision integer class also has a Javadoc:SE|java/math|BigInteger|bitCount() method that counts bits.
In Common Lisp, the function logcount, given a non-negative integer, returns the number of 1 bits. (For negative integers it returns the number of 0 bits in 2's complement notation.) In either case the integer can be a BIGNUM.
See also
*
Hamming distance
*Minimum weight External links
* [http://aggregate.ee.engr.uky.edu/MAGIC/#Population%20Count%20(Ones%20Count) Aggregate Magic Algorithms] . Optimized population count and other algorithms explained with sample code.
* [http://www.inwap.com/pdp10/hbaker/hakmem/hacks.html#item169 HACKMEM item 169] . Population count assembly code for the PDP/6-10.
* [http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetNaive Bit Twiddling Hacks] Several algorithms with code for counting bits set.
Wikimedia Foundation. 2010.