java.lang.Integer.bitCount()%uA0方法返回一个位在指定的int i值的二进制补码表示的数字。这有时被称为“人口数”。
声明
以下是java.lang.Integer.bitCount()方法的声明
public static int bitCount(int i)
参数
-
i%uA0-- 这是int值。
返回值
此方法返回一个位在指定int值的二进制补码表示的数字。
异常
-
NA
例子
下面的例子显示java.lang.Integer.bitCount()方法的使用。
package com.yiibai import java.lang.* public class IntegerDemo { public static void main(String[] args) { int i = 255 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 = 170 Binary = 10101010 Number of one bits = 4