/**
 * Counts number of 1 bits in a 32 bit unsigned number.
 * This method counts the number of bits set within the given
 * integer. Given an n-bit value with k of those bits set, the
 * efficiency of this algorithm is O(k) rather than the O(n) of
 * an algorithm that simply looped through all bits counting non
 * zero ones.
 *
 * @param x unsigned 32 bit number whose bits you wish to count.
 *
 * @return number of 1 bits in x.
 * @author Dale King KingD@TCE.com KingD@TCE.com
 */
public static int countBits3( int x )
   {
   int count = 0;
   while ( x != 0 )
      {
      // The result of this operation is to subtract off
      // the least significant non-zero bit. This can be seen
      // from noting that subtracting 1 from any number causes
      // all bits up to and including the least significant
      // non-zero bit to be complemented.
      //
      // For example:
      // 10101100 x
      // 10101011 x-1
      // 10101000 (x - 1) & x
      x &= x - 1;
      count++;
      }
   return count;
   }