📜  Java整数 bitCount() 方法

📅  最后修改于: 2022-05-13 01:54:20.495000             🧑  作者: Mango

Java整数 bitCount() 方法

Java.lang 包的 Integer 类的bitCount()方法返回 int 值的二进制补码表示中一位数的计数。这个函数有时被称为人口计数

句法 :

public static int bitCount(int n)
Parameter :
n : the value whose bits are to be counted
Return :
This method returns the count of the number of one-bits in the two's complement 
binary representation of an int value.

示例:显示Java.lang.Integer.bitCount()方法的工作。

// Java program to demonstrate working
// of java.lang.Integer.bitCount() method
import java.lang.Integer;
  
class Gfg {
    // driver code
    public static void main(String args[])
    {
        int a = 10;
  
        // Convert integer number to binary  format
        System.out.println(Integer.toBinaryString(a));
  
        // to print number of 1's in the number a
        System.out.println(Integer.bitCount(a));
    }
}

输出:

1010
2