package com.jldx.test;
public class TongjiTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int data = 25453;
System.out.println(tongji(data));
}
/**
* 统计整数的二进制表示中1的个数
* @param data
* @return 某整数的二进制表示中1的个数
*/
public static int tongji(int data) {
int count = 0;
System.out.println(Integer.toBinaryString(data));
while (data != 0)
{
data = data & (data-1);
System.out.println(Integer.toBinaryString(data));
count++;
}
return count;
}
}
输出结果:
110001101101101
110001101101100
110001101101000
110001101100000
110001101000000
110001100000000
110001000000000
110000000000000
100000000000000
0
9