查找输入整数二进制中1的个数

查找输入整数二进制中1的个数

http://www.nowcoder.com/questionTerminal/1b46eb4cf3fa49b9965ac3c2c1caf5ad

import java.util.Scanner;

public class Main {

    private int findNumberOf1( int num) {
        int count = 0;
        while (num != 0) {
            if ((num & 1) == 1) count++;
            num = num >>> 1;
        }
        return count;
    } 
    public Main() {
        Scanner in = new Scanner(System.in);
        while (in.hasNextInt()) {
            int num = in.nextInt();
            int res = findNumberOf1(num);
            System.out.println(res);
        }
    }

    public static void main(String[] args) {
        Main solution = new Main();
    }
}
全部评论

相关推荐

07-02 13:52
门头沟学院 Java
点赞 评论 收藏
分享
仁者伍敌:服务员还要脱颖而出,这是五星级酒店吗
点赞 评论 收藏
分享
评论
1
2
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务