位移加上与运算即可。

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextInt()) { 
            int a = in.nextInt();
            int count = 0;
            while(a>0){
                count+=a&1;
                a=a>>1;
            }
            System.out.println(count);
        }
    }
}