[编程题]求int型正整数在内存中存储时1的个数
求int型正整数在内存中存储时1的个数
http://www.nowcoder.com/questionTerminal/440f16e490a0404786865e99c6ad91c9
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); while(sc.hasNext()){ int num = sc.nextInt(); //计算1的个数 int count = 0; String toBinary = Integer.toBinaryString(num); for(int i = 0; i < toBinary.length(); i++){ char c = toBinary.charAt(i); if(c == '1'){ count++; } } System.out.println(count); } } }