查找输入整数二进制中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-10 11:55
点赞 评论 收藏
分享
点赞 评论 收藏
分享
陆续:不可思议 竟然没那就话 那就我来吧 :你是我在牛客见到的最美的女孩
点赞 评论 收藏
分享
07-08 13:48
门头沟学院 C++
点赞 评论 收藏
分享
评论
1
2
分享

创作者周榜

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