Java - Integer highestOneBit() 方法



描述

Java Integer highestOneBit() 方法返回一個 int 值,該值最多隻有一個位元位為 1,位於指定 int 值中最高位(“最左邊”)的 1 位元位的位置。如果指定值在其二進位制補碼錶示中沒有 1 位元位,即如果它等於零,則返回零。

宣告

以下是 java.lang.Integer.highestOneBit() 方法的宣告

public static int highestOneBit(int i)

引數

i − 這是 int 值。

返回值

此方法返回一個 int 值,該值只有一個位元位為 1,位於指定值中最高位 1 位元位的位置,或者如果指定值本身等於零,則返回零。

異常

從正整數中獲取最左邊 1 位元位的示例

以下示例演示瞭如何使用 Integer highestOneBit() 方法獲取一個 int 值,該值最多隻有一個位元位為 1,位於最高位。我們建立了一個 int 變數併為其分配了一個正整數。然後使用 toBinaryString() 方法,我們列印該值的二進位制格式。使用 bitCount(),我們列印 1 位元位的數量,然後使用 highestOneBit() 方法列印最高位 1 位元位的值。

package com.tutorialspoint;

public class IntegerDemo {
   public static void main(String[] args) {
      int i = 170;
      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));

      /* returns an int value with at most a single one-bit, in the position 
         of the highest-order ("leftmost") one-bit in the specified int value */ 
      System.out.println("Highest one bit = " + Integer.highestOneBit(i));
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Number = 170
Binary = 10101010
Number of one bits = 4
Highest one bit = 128

從負整數中獲取最左邊 1 位元位的示例

以下示例演示瞭如何使用 Integer highestOneBit() 方法獲取一個 int 值,該值最多隻有一個位元位為 1,位於最高位。我們建立了一個 int 變數併為其分配了一個負整數。然後使用 toBinaryString() 方法,我們列印該值的二進位制格式。使用 bitCount(),我們列印 1 位元位的數量,然後使用 highestOneBit() 方法列印最高位 1 位元位的值。

package com.tutorialspoint;

public class IntegerDemo {
   public static void main(String[] args) {
      int i = -170;
      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));

      /* returns an int value with at most a single one-bit, in the position 
         of the highest-order ("leftmost") one-bit in the specified int value */ 
      System.out.println("Highest one bit = " + Integer.highestOneBit(i));
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Number = -170
Binary = 11111111111111111111111101010110
Number of one bits = 28
Highest one bit = -2147483648

從正零值中獲取最左邊 1 位元位的示例

以下示例演示瞭如何使用 Integer highestOneBit() 方法獲取一個 int 值,該值最多隻有一個位元位為 1,位於最高位。我們建立了一個 int 變數併為其分配了一個正整數。然後使用 toBinaryString() 方法,我們列印該值的二進位制格式。使用 bitCount(),我們列印 1 位元位的數量,然後使用 highestOneBit() 方法列印最高位 1 位元位的值。

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));

      /* returns an int value with at most a single one-bit, in the position 
         of the highest-order ("leftmost") one-bit in the specified int value */ 
      System.out.println("Highest one bit = " + Integer.highestOneBit(i));
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Number = 0
Binary = 0
Number of one bits = 0
Highest one bit = 0
java_lang_integer.htm
廣告

© . All rights reserved.