public class Program
{
public static void Main()
var i = 6;
var c = BitCount(i);
System.Console.WriteLine(c);
}
// Method found here: https://stackoverflow.com/a/62264795/2228526
public static int BitCount(int n)
var count = 0;
while (n != 0)
count++;
n &= (n - 1); //walking through all the bits which are set to one
return count;