java.lang.Integer.numberOfLeadingZeros()是一種返回highest-order之前的零(0)位總數的方法(即,最左或最高有效“1”位)one-bit,以指定整數值或我們可以的二進製補碼表示說這是將int值轉換為Binary然後認為最高的一位並返回no的函數。前麵的零位。如果指定的整數值在其補碼表示形式中沒有one-bits,也就是說,如果它等於零,則將返回32。
用法:
public static int numberOfLeadingZeros(int arg)
參數:此方法接受單個參數arg,它是整數值。
返回值:此方法以指定的int值的二進製補碼表示形式返回highest-order set-bit之前的零位;如果該值等於零,則返回32。
說明:
- 考慮任何整數arg = 19
- 二進製表示= 0001 0011
- 最高位(5位),即0001 0000
- 因此結果= 16-5,即11
以下示例程序旨在說明java.lang.Integer.numberOfLeadingZeros()方法。
程序1:為正數。
// Java praogram to illustrate the
// java.lang.Integer.numberOfLeadingZeros()
import java.lang.*;
public class LeadingZeros {
public static void main(String[] args)
{
int a = 155;
System.out.println("Integral Number = " + a);
// Returns the number of zero bits preceding the highest-order
// leftmost one-bit
System.out.print("Number of Leading Zeros = ");
System.out.println(Integer.numberOfLeadingZeros(a));
a = 10;
System.out.println("Integral Number = " + a);
// Returns the number of zero bits preceding the highest-order
// leftmost one-bit
System.out.print("Number of Leading Zeros = ");
System.out.println(Integer.numberOfLeadingZeros(a));
}
}
輸出:
Integral Number = 155 Number of Leading Zeros = 24 Integral Number = 10 Number of Leading Zeros = 28
程序2:為負數。
// Java praogram to illustrate the
// java.lang.Integer.numberOfLeadingZeros()
import java.lang.*;
public class LeadingZeros {
public static void main(String[] args)
{
int a = -15;
System.out.println("Number = " + a);
// Returns the number of zero bits preceding the highest-order
// leftmost one-bit
System.out.print("Number of Leading Zeros = ");
System.out.println(Integer.numberOfLeadingZeros(a));
}
}
輸出:
Number = -15 Number of Leading Zeros = 0
程序3:對於十進製值。
注意:當將十進製值作為參數傳遞時,它將返回錯誤消息。
// Java praogram to illustrate the
// java.lang.Integer.numberOfLeadingZeros()
import java.lang.*;
public class LeadingZeros {
public static void main(String[] args)
{
// Returns the number of zero bits preceding the highest-order
// leftmost one-bit
System.out.print("Number of Leading Zeros = ");
System.out.println(Integer.numberOfLeadingZeros(16.32));
}
}
輸出:
prog.java:11:error:incompatible types:possible lossy conversion from double to int System.out.println(Integer.numberOfLeadingZeros(16.32)); ^ Note:Some messages have been simplified; recompile with -Xdiags:verbose to get full output 1 error
程序4:對於字符串值,請在參數中傳遞。
注意:當將字符串作為參數傳遞時,它將返回錯誤消息。
// Java praogram to illustrate the
// java.lang.Integer.numberOfLeadingZeros()
import java.lang.*;
public class LeadingZeros {
public static void main(String[] args)
{
// returns the number of zero bits preceding the highest-order
// leftmost one-bit
System.out.print("Number of Leading Zeros = ");
System.out.println(Integer.numberOfLeadingZeros("18"));
}
}
輸出:
prog.java:11:error:incompatible types:String cannot be converted to int System.out.println(Integer.numberOfLeadingZeros("18")); ^ Note:Some messages have been simplified; recompile with -Xdiags:verbose to get full output 1 error
相關用法
- Java Java lang.Long.lowestOneBit()用法及代碼示例
- Java Java lang.Long.numberOfLeadingZeros()用法及代碼示例
- Java Java lang.Long.highestOneBit()用法及代碼示例
- Java Java.util.Collections.rotate()用法及代碼示例
- Java Java lang.Long.byteValue()用法及代碼示例
- Java Java lang.Long.numberOfTrailingZeros()用法及代碼示例
- Java Java lang.Long.reverse()用法及代碼示例
- Java Java lang.Long.builtcount()用法及代碼示例
- Java Java.util.Collections.disjoint()用法及代碼示例
- Java Clock withZone()用法及代碼示例
- Java Java.lang.string.replace()用法及代碼示例
- Java Java.util.ArrayList.addall()用法及代碼示例
- Java Clock tickMinutes()用法及代碼示例
注:本文由純淨天空篩選整理自ankita_chowrasia大神的英文原創作品 Integer.numberOfLeadingZeros() Method in Java With Example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。