Integer類numberOfLeadingZeros()方法
- numberOfLeadingZeros() 方法可在
java.lang
包。 - numberOfLeadingZeros() 方法用於返回整數類型的給定參數 [value] 的 2 的補碼中最左邊一位之前的 0 位數。否則,如果給定的參數值為 0,則返回 32。
- numberOfLeadingZeros() 方法是一個靜態方法,它也可以通過類名訪問,如果我們嘗試使用類對象訪問該方法,那麽我們也不會收到錯誤。
- numberOfLeadingZeros() 方法不拋出異常。
用法:
public static int numberOfLeadingZeros (int value);
參數:
int value
– 表示要解析的整數值。
返回值:
這個方法的返回類型是int
, 如果給定的參數不為零,則返回給定 int 值最左邊一位之前的 0 位的數量。否則,如果給定的參數為零,則返回值 32。
例:
// Java program to demonstrate the example
// of numberOfLeadingZeros (int value) method of Integer class
public class NumberOfLeadingZerosOfIntegerClass {
public static void main(String[] args) {
int value1 = 1296;
int value2 = 0;
// It returns the string representation of the given unsigned
// integer value denoted by the argument in binary by calling
// Integer.toBinaryString(value1)
System.out.println("Integer.toBinaryString(value1):" + Integer.toBinaryString(value1));
// It returns the string representation of the given unsigned
// integer value denoted by the argument in binary by calling
// Integer.toBinaryString(value2)
System.out.println("Integer.toBinaryString(value2):" + Integer.toBinaryString(value2));
// It returns the number of 0's bits preceding the leftmost side
// one bit in the given argument 'value' by calling
// Integer.numberOfLeadingZeros(value1)
System.out.println("Integer.numberOfLeadingZeros(value1):" + Integer.numberOfLeadingZeros(value1));
// It returns the value 32 because the value of
// the given argument is zero
System.out.println("Integer.numberOfLeadingZeros(value2):" + Integer.numberOfLeadingZeros(value2));
}
}
輸出
Integer.toBinaryString(value1):10100010000 Integer.toBinaryString(value2):0 Integer.numberOfLeadingZeros(value1):21 Integer.numberOfLeadingZeros(value2):32
相關用法
- Java Integer numberOfLeadingZeros()用法及代碼示例
- Java Integer numberOfTrailingZeros()用法及代碼示例
- Java Integer doubleValue()用法及代碼示例
- Java Integer max()用法及代碼示例
- Java Integer intValue()用法及代碼示例
- Java Integer floatValue()用法及代碼示例
- Java Integer toUnsignedLong()用法及代碼示例
- Java Integer parseUnsignedInt()用法及代碼示例
- Java Integer reverseBytes()用法及代碼示例
- Java Integer shortValue()用法及代碼示例
- Java Integer compare()用法及代碼示例
- Java Integer byteValue()用法及代碼示例
- Java Integer min()用法及代碼示例
- Java Integer getInteger()用法及代碼示例
- Java Integer reverseBytes(int i)用法及代碼示例
- Java Integer toUnsignedString()用法及代碼示例
- Java Integer compareTo()用法及代碼示例
- Java Integer remainderUnsigned()用法及代碼示例
- Java Integer decode()用法及代碼示例
- Java Integer equals()用法及代碼示例
注:本文由純淨天空篩選整理自Preeti Jain大神的英文原創作品 Java Integer class numberOfLeadingZeros() method with example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。