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.