static int bitCount(int i)
描述 (Description)
java.lang.Integer.bitCount()方法返回指定int值i二进制补码二进制表示中的一位数。 这有时被称为population count 。
声明 (Declaration)
以下是java.lang.Integer.bitCount()方法的声明
public static int bitCount(int i)
参数 (Parameters)
i - 这是int值。
返回值 (Return Value)
此方法返回指定int值的二进制补码二进制表示中的一位数。
异常 (Exception)
NA
例子 (Example)
以下示例显示了java.lang.Integer.bitCount()方法的用法。
package com.iowiki;
import java.lang.*;
public class IntegerDemo {
public static void main(String[] args) {
int i = 177;
System.out.println("Number = " + i);
/* returns the string representation of the unsigned integer 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));
}
}
让我们编译并运行上面的程序,这将产生以下结果 -
Number = 177
Binary = 10110001
Number of one bits = 4