java.lang.Integer.bitCount()和java.lang.Long.bitCount()方法返回二进制表示中1的位数。有时将此功能称为人口计数。
public class BitCountDemo {
public static void main(String[] args) {
int i = 219;
System.out.println("Number = " + i);
/*
* returns the string representation of the unsigned long value represented by
* the argument in binary (base 2)
*/
System.out.println("Binary = " + Integer.toBinaryString(i));
// returns the number of one-bits
System.out.println("Number of one bits = " + Integer.bitCount(i));
long l = 219;
System.out.println("Number = " + l);
/*
* returns the string representation of the unsigned long value represented by
* the argument in binary (base 2)
*/
System.out.println("Binary = " + Long.toBinaryString(l));
// returns the number of one-bits
System.out.println("Number of one bits = " + Long.bitCount(l));
}
}
Number = 219
Binary = 11011011
Number of one bits = 6
Number = 219
Binary = 11011011
Number of one bits = 6