Java - Integer bitCount() 方法



描述

Java Integer bitCount() 方法返回指定 int 值 i 的二进制补码表示中 1 的位数。这有时被称为统计置位

声明

以下是 java.lang.Integer.bitCount() 方法的声明

public static int bitCount(int i)

参数

i − 这是一个 int 值。

返回值

此方法返回指定 int 值的二进制补码表示中 1 的位数。

异常

正整数中 1 的位数示例

以下示例演示了如何使用 Integer bitCount() 方法获取整数中 1 的位数。我们创建了一个 int 变量并为其赋值一个正值。然后使用 toBinaryString() 方法,我们打印二进制表示和使用 bitCount() 方法的位计数。

package com.tutorialspoint;
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

负整数中 1 的位数示例

以下示例演示了如何使用 Integer bitCount() 方法获取整数中 1 的位数。我们创建了一个 int 变量并为其赋值一个负值。然后使用 toBinaryString() 方法,我们打印二进制表示和使用 bitCount() 方法的位计数。

package com.tutorialspoint;
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 = 11111111111111111111111101001111
Number of one bits = 29

正零整数中 1 的位数示例

以下示例演示了如何使用 Integer bitCount() 方法获取整数中 1 的位数。我们创建了一个 int 变量并为其赋值零值。然后使用 toBinaryString() 方法,我们打印二进制表示和使用 bitCount() 方法的位计数。

package com.tutorialspoint;
public class IntegerDemo {
   public static void main(String[] args) {
      int i = 0;
      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 = 0
Binary = 0
Number of one bits = 0

负零整数中 1 的位数示例

以下示例演示了如何使用 Integer bitCount() 方法获取整数中 1 的位数。我们创建了一个 int 变量并为其赋值负零值。然后使用 toBinaryString() 方法,我们打印二进制表示和使用 bitCount() 方法的位计数。

package com.tutorialspoint;
public class IntegerDemo {
   public static void main(String[] args) {

      int i = -0;
      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 = 0
Binary = 0
Number of one bits = 0
java_lang_integer.htm
广告
© . All rights reserved.