📌  相关文章
📜  Java lang.Long.numberOfLeadingZeros() 方法在Java中的例子

📅  最后修改于: 2022-05-13 01:54:44.159000             🧑  作者: Mango

Java lang.Long.numberOfLeadingZeros() 方法在Java中的例子


Java .lang.Long.numberOfLeadingZeros()是Java中的一个内置函数,它返回最高位设置位左侧的前导零位的数量。简单来说,它返回(64-position) ,其中 position 是指从右边开始的最高位。如果数字不包含任何设置位(换句话说,如果数字为零),则返回 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()函数:

方案一:

// 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"));