/**
* Quickly calculates the number of 1 in a binary number (fast Bit counting)
* The idea of the algorithm is as follows:
* Each time you subtract the number from that number, the rightmost 1 is eliminated
* until the number is 0
* The number of intermediate loops is 1 of the number
* For example, given "10100", minus one after "10011", and "10000", which will eliminate the rightmost 1
* Sparse ones and dense ones were first Described by Peter Wegner in
* "a technique for counting ones in a Binary Computer",
* Communications of the ACM, Volume 3 (1960) Number 5, page 322
/
package al;
public class Countones {public
static void Main (string[] args) {
int i = 7;
Countones count = new Countones ();
System.out.println ("There are" + count.getcount (i) + "ones in I");
}
/**
* @author *
@param i to be measured number
* @return binary representation of 1 of the number of * * Public
int getcount (int i) {
int n;
for (n=0 i > 0; n++) {
i &= (i-1);
}
return n;
}
}