java.lang.Long.numberOfLeadingZeros()是Java中的內置函數,它返回最高順序設置位左側的前導零位的數量。簡單來說,它返回(64位置),其中位置是指從右起的最高順序設置位。如果該數字不包含任何設置位(換句話說,如果該數字為零),則返回64。
用法:
public static long numberOfLeadingZeros(long num) Parameters: num - the number passed Returns: the number of leading zeros before the highest-order set bit
例子:
Input:8 Output:60 Explanation:Binary representation of 8 is 1000 No of leading zeros to the left of the highest-order set bit is 60. Input:25 Output:59
以下示例程序旨在說明java.lang.Long.numberOfLeadingZeros()函數:
程序1:
// Java program that demonstrates the
// Long.numberOfLeadingZeros() function
// include lang package
import java.lang.*;
public class GFG {
public static void main(String[] args)
{
long l = 8;
// returns the number of zero bits before the highest-order
// set bit
System.out.println("Number of leading zeros = "
+ Long.numberOfLeadingZeros(l));
// second example
l = 25;
System.out.println("Number of leading zeros = "
+ Long.numberOfLeadingZeros(l));
}
}
輸出:
Number of leading zeros = 60 Number of leading zeros = 59
注意:如果是負數,則每個數字都將有0個前導零。
程序2:下麵的程序演示了傳遞負數時函數的使用。
// Java program that demonstrates the
// Long.numberOfLeadingZeros() function
// negative number
// include lang package
import java.lang.*;
public class GFG {
public static void main(String[] args)
{
long l = -12;
// returns the number of zero bits before the highest-order
// set bit
System.out.println("Number of leading zeros = "
+ Long.numberOfLeadingZeros(l));
l = -100;
System.out.println("Number of leading zeros = "
+ Long.numberOfLeadingZeros(l));
}
}
輸出:
Number of leading zeros = 0 Number of leading zeros = 0
程序3:當將十進製字符串值作為參數傳遞時,它將返回錯誤消息。
// Java program that demonstrates the
// Long.numberOfLeadingZeros() function
// decimal number
// include lang package
import java.lang.*;
public class GFG {
public static void main(String[] args)
{
// returns the number of zero bits before the highest-order
// set bit
System.out.println("Number of leading zeros = "
+ Long.numberOfLeadingZeros(10.45));
}
}
輸出:
prog.java:16:error:incompatible types:possible lossy conversion from double to long + Long.numberOfLeadingZeros(10.45));
程序4:在參數中傳遞字符串值時。
// Java program that demonstrates the
// Long.numberOfLeadingZeros() function
// string number
// include lang package
import java.lang.*;
public class GFG {
public static void main(String[] args)
{
// returns the number of zero bits before the highest-order
// set bit
System.out.println("Number of leading zeros = "
+ Long.numberOfLeadingZeros("10"));
}
}
輸出:
prog.java:16:error:incompatible types:String cannot be converted to long + Long.numberOfLeadingZeros("10"));
相關用法
- Java Java lang.Long.highestOneBit()用法及代碼示例
- Java Java lang.Long.builtcount()用法及代碼示例
- Java Java lang.Long.reverse()用法及代碼示例
- Java Java lang.Long.lowestOneBit()用法及代碼示例
- Java Java.util.Collections.disjoint()用法及代碼示例
- Java Java lang.Long.numberOfTrailingZeros()用法及代碼示例
- Java Java.util.Collections.rotate()用法及代碼示例
- Java Java lang.Long.byteValue()用法及代碼示例
- Java Clock withZone()用法及代碼示例
- Java Clock tickMinutes()用法及代碼示例
- Java Set contains()用法及代碼示例
- Java Set add()用法及代碼示例
- Java Map get()用法及代碼示例
注:本文由純淨天空篩選整理自gopaldave大神的英文原創作品 Java lang.Long.numberOfLeadingZeros() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。