Java - Long bitCount() 方法



描述

Java Long bitCount() 方法返回指定长整型值 i 的二进制补码表示中1的个数。这有时被称为种群计数

声明

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

public static long  bitCount(long  i)

参数

i − 这是长整型值。

返回值

此方法返回指定长整型值的二进制补码表示中1的个数。

异常

正值长整型获取1的个数示例

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

package com.tutorialspoint;
public class LongDemo {
   public static void main(String[] args) {
      long  i = 177L;
      System.out.println("Number = " + i);
    
      /* returns the string representation of the unsigned long value 
         represented by the argument in binary (base 2) */
      System.out.println("Binary = " + Long.toBinaryString(i));

      // returns the number of one-bits 
      System.out.println("Number of one bits = " + Long.bitCount(i)); 
   }
} 

输出

让我们编译并运行上述程序,这将产生以下结果:

Number = 177
Binary = 10110001
Number of one bits = 4

负值长整型获取1的个数示例

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

package com.tutorialspoint;
public class LongDemo {
   public static void main(String[] args) {
      long  i = -177L;
      System.out.println("Number = " + i);
    
      /* returns the string representation of the unsigned long value 
         represented by the argument in binary (base 2) */
      System.out.println("Binary = " + Long.toBinaryString(i));

      // returns the number of one-bits 
      System.out.println("Number of one bits = " + Long.bitCount(i)); 
   }
} 

输出

让我们编译并运行上述程序,这将产生以下结果:

Number = -177
Binary = 1111111111111111111111111111111111111111111111111111111101001111
Number of one bits = 61

零值长整型获取1的个数示例

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

package com.tutorialspoint;
public class LongDemo {
   public static void main(String[] args) {
      long  i = 0L;
      System.out.println("Number = " + i);
    
      /* returns the string representation of the unsigned long value 
         represented by the argument in binary (base 2) */
      System.out.println("Binary = " + Long.toBinaryString(i));

      // returns the number of one-bits 
      System.out.println("Number of one bits = " + Long.bitCount(i)); 
   }
} 

输出

让我们编译并运行上述程序,这将产生以下结果:

Number = 0
Binary = 0
Number of one bits = 0

负零值长整型获取1的个数示例

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

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

      long  i = -0L;
      System.out.println("Number = " + i);
    
      /* returns the string representation of the unsigned long value 
         represented by the argument in binary (base 2) */
      System.out.println("Binary = " + Long.toBinaryString(i));

      // returns the number of one-bits 
      System.out.println("Number of one bits = " + Long.bitCount(i)); 
   }
} 

输出

让我们编译并运行上述程序,这将产生以下结果:

Number = 0
Binary = 0
Number of one bits = 0
java_lang_long.htm
广告
© . All rights reserved.