java.lang.Integer.bitCount()方法返回指定int值的二进制补码二进制表示形式中的一位数。
首先,设置一个int值-
int val = 210;
现在,找到一位的数量-
Integer.bitCount(val)
以下是在Java中实现bitCount()方法的示例-
public class Main {
public static void main(String[] args) {
int val = 210;
System.out.println("Number = " + val);
System.out.println("Binary = " + Integer.toBinaryString(val));
// returns the number of one-bits
System.out.println("Number of one bits = " + Integer.bitCount(val));
}
}输出结果
Number = 210 Binary = 11010010 Number of one bits = 4