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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。