java.lang的Integer.highestOneBit()方法返回一個整數值,該整數值最多具有單個one-bit,對於給定的int值,其位於highest-order(即最左側)one-bit的位置。如果指定的值在其補碼二進製表示形式中不包含one-bits,則其返回零(簡單地等於0)。
用法:
public static int highestOneBit(int a)
參數:該方法采用整數類型的一個參數a,該參數表示要返回其最高順序位或對其執行操作的值。
返回值:該方法可以返回兩種類型的值:
- 在指定值的highest-order one-bit的位置中返回一個1位整數值
- 如果指定值等於零,則返回零。
例子:
Input: 19 Output: Highest one bit of the given integer is = 16 Input: -9 Output: Highest one bit of the given integer is = -2147483648 Explanation: Consider any integer a = 19 Binary Representation = 0001 0011 Highest bit(at 4) i.e.0001 0000 so result = 2^4=16
以下示例程序旨在說明Java.lang.Integer.highestOneBit()方法。
示例1:為正數。
// Java praogram to illustrate the
// Java.lang.Integer.highestOneBit() Method
import java.lang.*;
public class HighestOneBit {
public static void main(String[] args)
{
int a = 29;
System.out.println("Number is = " + a);
// returns an int value with at most a single one-bit, in the position
// of the highest-order or the leftmost one-bit in the specified int value
System.out.print("Highest one bit of the given integer is = ");
System.out.println(Integer.highestOneBit(a));
a = 44;
System.out.println("Number is = " + a);
System.out.print("Highest one bit of the given integer is = ");
System.out.println(Integer.highestOneBit(a));
}
}
輸出:
Number is = 29 Highest one bit of the given integer is = 16 Number is = 44 Highest one bit of the given integer is = 32
示例2:為負數。
// Java praogram to illustrate the
// Java.lang.Integer.highestOneBit() Method
import java.lang.*;
public class HighestOneBit {
public static void main(String[] args)
{
int a = -9;
System.out.println("Number is = " + a);
// returns an int value with at most a single one-bit, in the position
// of the highest-order or the leftmost one-bit in the specified int value
System.out.print("Highest one bit of the given integer is = ");
System.out.println(Integer.highestOneBit(a));
}
}
輸出:
Number is = -9 Highest one bit of the given integer is = -2147483648
示例3:對於十進製值。
注意:由於類型不兼容而將十進製值作為參數傳遞時,它將返回錯誤消息。
// Java praogram to illustrate the
// Java.lang.Integer.highestOneBit() Method
import java.lang.*;
public class HighestOneBit {
public static void main(String[] args)
{
int a = 84.22;
System.out.println("Number is = " + a);
// decimal value 84.22 is passed here
System.out.print("Highest one bit of the given integer is = ");
System.out.println(Integer.highestOneBit(a));
}
}
輸出:
prog.java:9: error: incompatible types: possible lossy conversion from double to int int a = 84.22; ^ 1 error
示例4:對於字符串。
注意:由於類型不兼容而將字符串作為參數傳遞時,它將返回錯誤消息。
// Java praogram to illustrate the
// Java.lang.Integer.highestOneBit() Method
import java.lang.*;
public class HighestOneBit {
public static void main(String[] args)
{
int a = "34";
System.out.println("Number is = " + a);
// decimal value 84.22 is passed here
System.out.print("Highest one bit of the given integer is = ");
System.out.println(Integer.highestOneBit(a));
}
}
輸出:
prog.java:9: error: incompatible types: String cannot be converted to int int a = "34"; ^ 1 error
相關用法
- Java Java lang.Long.highestOneBit()用法及代碼示例
- Java Integer sum()用法及代碼示例
- Java Integer lowestOneBit()用法及代碼示例
- Java Integer byteValue()用法及代碼示例
- Java Integer.numberOfTrailingZeros()用法及代碼示例
- Java Integer.numberOfLeadingZeros()用法及代碼示例
- Java Integer doubleValue()用法及代碼示例
- Java Integer bitCount()用法及代碼示例
- Java Integer floatValue()用法及代碼示例
- Java Integer toOctalString()用法及代碼示例
- Java Integer compareUnsigned()用法及代碼示例
- Java Integer compare()用法及代碼示例
- Java Integer compareTo()用法及代碼示例
- Java Integer hashCode()用法及代碼示例
- Java Integer signum()用法及代碼示例
注:本文由純淨天空篩選整理自ankita_chowrasia大神的英文原創作品 Integer highestOneBit() Method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。