统计给二进制数有多少个1

思路

任何数和1进行与操作,都保留其末尾数

  private static void oneCount(int i) {
        int count = 0;
        while (i > 0) {
            int one = i & 1;
            if (one == 1) {
                count++;
            }
            i = i >> 1;
        }
        System.out.println(":count:" + count);
    }

猜你喜欢

转载自www.cnblogs.com/dongma/p/13169230.html