比如: 15 :0000 1111 4 个 1
方法:按位与
public class Test{
public static int Test(int num){
int count=0;
while(num>0){
if((num & 1) ==1){
count++;
}
num=num>>1;
}
return count;
}
public static void main(String[] args) {
int i=666;
System.out.println(Integer.toBinaryString(i));
System.out.println(Test(i));
}
}