numberOfLeadingZeros() 方法是java.lang 包下Integer 类的一个方法。此方法返回指定整数值的二进制补码表示中 highest-order ("leftmost") one-bit 之前的零位总数,即将 int 值转换为 Binary 然后考虑最高一位并返回其前面的零位总数。换句话说,如果指定的整数值没有 one-bits 或在其二进制补码表示中等于 0,则它将返回 32。
注意:此方法与以 2 为底的对数密切相关。对于所有正整数值 x:
floor(log2(x)) = 31 - numberOfLeadingZeros(x)
ceil(log2(x)) = 32 - numberOfLeadingZeros(x - 1).
说明:
Suppose, Integer Value = 55
Binary Equivalent = 110111
Highest(Left-Most Bit) = 100000 i.e. at position 6
numberOfLeadingZeros = It will return in 32-bit representation, so preceding zero's are ( 32 - 6 = 26 ).
用法:
以下是 numberOfLeadingZeros() 方法的声明:
public static int numberOfLeadingZeros(int i)
参数:
数据类型 | 参数 | 描述 | 必需/可选 |
---|---|---|---|
int | i | 它接受整数值,该值返回其 2 的补码的最高位。 | Required |
返回值:
numberOfLeadingZeros() 方法返回指定整数值的二进制补码表示中 highest-order ("leftmost") one-bit 之前的零位数,如果该值等于零,则返回 32。
异常:
NA
兼容版本:
Java 1.5 及以上
例子1
public class IntegerNumberOfLeadingZerosExample1 {
public static void main(String[] args) {
System.out.println("Leading Zero's:"+Integer.numberOfLeadingZeros(10));
}
}
输出:
Leading Zero's:28
例子2
public class IntegerNumberOfLeadingZerosExample2 {
public static void main(String[] args) {
int value = 55;
//Get the binary equivalent of this Integer value
System.out.print("Binary equivalent:"+Integer.toBinaryString(value));
//Print the number of Leading zeros
System.out.print("\nNumber of Leading Zeros:"+Integer.numberOfLeadingZeros(value));
}
}
输出:
Binary equivalent:110111 Number of Leading Zeros:26
例子3
import java.util.Scanner;
public class IntegerNumberOfLeadingZerosExample3 {
public static void main(String[] args) {
//Get Numeric value from Console
System.out.print("Enter the desired Integer value:");
Scanner readInput = new Scanner(System.in);
int value = readInput.nextInt();
readInput.close();
//Get the binary equivalent of this Integer value
System.out.print("Binary equivalent:"+Integer.toBinaryString(value));
//Print the number of leading zeros
System.out.print("\nNumber of Leading Zeros:"+Integer.numberOfLeadingZeros(value));
}
}
输出:
Enter the desired Integer value:75 Binary equivalent:1001011 Number of Leading Zeros:25
示例 4
public class IntegerNumberOfLeadingZerosExample4 {
public static void main(String[] args) {
int num = -15;
System.out.print("Input Number = " + num);
// Returns the number of zero bits preceding the highest leftmost one-bit
System.out.print("\nNumber of Leading Zeros = "+Integer.numberOfLeadingZeros(num));
}
}
输出:
Input Number = -15 Number of Leading Zeros = 0
例 5
public class IntegerNumberOfLeadingZerosExample5 {
public static void main(String[] args) {
System.out.println("Leading Zero's:"+Integer.numberOfLeadingZeros(0));
}
}
输出:
Leading Zero's:32
相关用法
- Java Integer numberOfTrailingZeros()用法及代码示例
- Java Integer doubleValue()用法及代码示例
- Java Integer max()用法及代码示例
- Java Integer intValue()用法及代码示例
- Java Integer floatValue()用法及代码示例
- Java Integer toUnsignedLong()用法及代码示例
- Java Integer parseUnsignedInt()用法及代码示例
- Java Integer reverseBytes()用法及代码示例
- Java Integer shortValue()用法及代码示例
- Java Integer compare()用法及代码示例
- Java Integer byteValue()用法及代码示例
- Java Integer min()用法及代码示例
- Java Integer getInteger()用法及代码示例
- Java Integer reverseBytes(int i)用法及代码示例
- Java Integer toUnsignedString()用法及代码示例
- Java Integer compareTo()用法及代码示例
- Java Integer remainderUnsigned()用法及代码示例
- Java Integer decode()用法及代码示例
- Java Integer equals()用法及代码示例
- Java Integer signum()用法及代码示例
注:本文由纯净天空筛选整理自 Java Integer numberOfLeadingZeros() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。