思路:该数和1进行与操作,得到尾数值

判断尾数是否为1,进行记录,直到右移完成

  private static void oneCount(int n){
        int count= 0;
        while (n > 0){
            int end = n & 1;
            if(end == 1){
                count++;
            }
            n = n >> 1;
        }
        System.out.println(count);

    }

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-11-20
  • 2022-12-23
  • 2022-12-23
  • 2021-09-19
  • 2021-09-02
  • 2021-06-21
猜你喜欢
  • 2021-11-05
  • 2022-12-23
  • 2022-02-17
  • 2021-12-16
  • 2021-07-23
  • 2021-07-31
  • 2022-12-23
相关资源
相似解决方案