/** * Counts number of 1 bits in a 32 bit unsigned number. * * @param x unsigned 32 bit number whose bits you wish to count. * * @return number of 1 bits in x. * @author Roedy Green email */ public static int countBits2(int x ) { // classic shift method int result = 0; for ( int i=0; i<32; i++ ) { result += x & 1; x >>>= 1; } return result; }