#include <iostream>
//this test will output the result and return number of iterations required
int method1( int n ); //simple - bitwise
int method2( int n ); //Kernigan's method

int main() {
	int n = 3526; //0110111000110 - 7 bits set
	int it1 = method1(n), it2 = method2(n); //iterations required
	
	std::cout << "Iterations required method1: " << it1 << std::endl;
	std::cout << "Iterations required method2: " << it2 << std::endl;
	
	return 0;
}

int method1( int n )
{
	int count = 0 , i;
	for( i = 0; n; ++i )
	{
		count += n & 1;
		n >>= 1;
	}
	
	std::cout << "Bits set: " << count << std::endl;
	return( i);
	
}

int method2( int n )
{
	int i;
	for( i = 0; n; ++i )
	{
		n &= n - 1;
	}
	std::cout << "Bits set: " << i << std::endl;
	return( i );
}
