Java中的 Integer.numberOfLeadingZeros() 方法及示例
Java.lang.Integer.numberOfLeadingZeros()是返回二进制补码表示中最高位(即最左或最高有效“1”位)一位之前的零(0)位总数的方法指定的整数值,或者我们可以说它是将 int 值转换为二进制然后考虑最高一位并返回 no 的函数。它前面的零位。如果指定的整数值在其二进制补码表示中没有一位,换句话说,如果它等于 0,那么它将返回 32。
句法 :
public static int numberOfLeadingZeros(int arg)
参数:此方法接受单个参数arg ,它是整数值。
返回值:此方法返回指定 int 值的二进制补码表示中最高设置位之前的零位数,如果该值等于 0,则返回 32。
解释:
- 考虑任何整数 arg = 19
- 二进制表示 = 0001 0011
- 最高位(在 5 处)即 0001 0000
- 所以结果 = 16-5 即 11
下面的程序说明了Java.lang.Integer.numberOfLeadingZeros() 方法。
程序 1:对于正数。
java
// Java program 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));
}
}
java
// Java program 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));
}
}
java
// Java program 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));
}
}
java
// Java program 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"));
}
}
输出:
Integral Number = 155
Number of Leading Zeros = 24
Integral Number = 10
Number of Leading Zeros = 28
程序 2:对于负数。
Java
// Java program 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
// Java program 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
// Java program 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