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.