How to count number of 1's in a binary number ?

public static void cntBit(int n)
{
        int cnt = 0;
        while(n!=0)
        {
                if((n & 1) > 0)
                {
                        cnt++;
                }
               
                n = n>> 1;
        }
        System.out.println(cnt);
}
Interview Questions: 

Search