- Java.lang 包类
- Java.lang - 首页
- Java.lang - Boolean
- Java.lang - Byte
- Java.lang - Character
- Java.lang - Character.Subset
- Java.lang - Character.UnicodeBlock
- Java.lang - Class
- Java.lang - ClassLoader
- Java.lang - Compiler
- Java.lang - Double
- Java.lang - Enum
- Java.lang - Float
- Java.lang - InheritableThreadLocal
- Java.lang - Integer
- Java.lang - Long
- Java.lang - Math
- Java.lang - Number
- Java.lang - Object
- Java.lang - Package
- Java.lang - Process
- Java.lang - ProcessBuilder
- Java.lang - Runtime
- Java.lang - RuntimePermission
- Java.lang - SecurityManager
- Java.lang - Short
- Java.lang - StackTraceElement
- Java.lang - StrictMath
- Java.lang - String
- Java.lang - StringBuffer
- Java.lang - StringBuilder
- Java.lang - System
- Java.lang - Thread
- Java.lang - ThreadGroup
- Java.lang - ThreadLocal
- Java.lang - Throwable
- Java.lang - Void
- Java.lang 包其他内容
- Java.lang - 接口
- Java.lang - 错误
- Java.lang - 异常
- Java.lang 包有用资源
- Java.lang - 有用资源
- Java.lang - 讨论
Java - Integer numberOfLeadingZeros() 方法
描述
Java Integer numberOfLeadingZeros() 方法返回指定 int 值的二进制补码表示中最高位(“最左边”)的 1 位之前的 0 位的数量。
如果指定值在其二进制补码表示中没有 1 位,换句话说,如果它等于零,则返回 32。
声明
以下是 java.lang.Integer.numberOfLeadingZeros() 方法的声明
public static int numberOfLeadingZeros(int i)
参数
i - 这是 int 值。
返回值
此方法返回指定 int 值的二进制补码表示中最高位(“最左边”)的 1 位之前的 0 位的数量,如果该值为零,则返回 32。
异常
无
从正 int 值获取前导零数量示例
以下示例演示了如何使用 Integer numberOfLeadingZeros() 方法获取最高位 1 之前的 0 位的数量。我们创建了一个 int 变量并为其分配了一个正 int 值。然后使用 toBinaryString() 方法,我们打印该值的二进制格式。使用 bitCount(),我们打印 1 位的数量。使用 highestOneBit(),我们打印最高位。使用 lowestOneBit(),我们打印最低位,然后使用 numberOfLeadingZeros() 方法打印最高位 1 之前的 0 位的值。
package com.tutorialspoint; public class IntegerDemo { public static void main(String[] args) { int i = 170; System.out.println("Number = " + i); /* returns the string representation of the unsigned integer value represented by the argument in binary (base 2) */ System.out.println("Binary = " + Integer.toBinaryString(i)); // returns the number of one-bits System.out.println("Number of one bits = " + Integer.bitCount(i)); /* returns an int value with at most a single one-bit, in the position of the highest-order ("leftmost") one-bit in the specified int value */ System.out.println("Highest one bit = " + Integer.highestOneBit(i)); /* returns an int value with at most a single one-bit, in the position of the lowest-order ("rightmost") one-bit in the specified int value.*/ System.out.println("Lowest one bit = " + Integer.lowestOneBit(i)); /*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(i)); } }
输出
让我们编译并运行以上程序,这将产生以下结果:
Number = 170 Binary = 10101010 Number of one bits = 4 Highest one bit = 128 Lowest one bit = 2 Number of leading zeros = 24
从负 int 值获取前导零数量示例
以下示例演示了如何使用 Integer numberOfLeadingZeros() 方法获取最高位 1 之前的 0 位的数量。我们创建了一个 int 变量并为其分配了一个负 int 值。然后使用 toBinaryString() 方法,我们打印该值的二进制格式。使用 bitCount(),我们打印 1 位的数量。使用 highestOneBit(),我们打印最高位。使用 lowestOneBit(),我们打印最低位,然后使用 numberOfLeadingZeros() 方法打印最高位 1 之前的 0 位的值。
package com.tutorialspoint; public class IntegerDemo { public static void main(String[] args) { int i = -170; System.out.println("Number = " + i); /* returns the string representation of the unsigned integer value represented by the argument in binary (base 2) */ System.out.println("Binary = " + Integer.toBinaryString(i)); // returns the number of one-bits System.out.println("Number of one bits = " + Integer.bitCount(i)); /* returns an int value with at most a single one-bit, in the position of the highest-order ("leftmost") one-bit in the specified int value */ System.out.println("Highest one bit = " + Integer.highestOneBit(i)); /* returns an int value with at most a single one-bit, in the position of the lowest-order ("rightmost") one-bit in the specified int value.*/ System.out.println("Lowest one bit = " + Integer.lowestOneBit(i)); /*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(i)); } }
输出
让我们编译并运行以上程序,这将产生以下结果:
Number = -170 Binary = 11111111111111111111111101010110 Number of one bits = 28 Highest one bit = -2147483648 Lowest one bit = 2 Number of leading zeros = 0
从正零 int 值获取前导零数量示例
以下示例演示了如何使用 Integer numberOfLeadingZeros() 方法获取最高位 1 之前的 0 位的数量。我们创建了一个 int 变量并为其分配了一个零 int 值。然后使用 toBinaryString() 方法,我们打印该值的二进制格式。使用 bitCount(),我们打印 1 位的数量。使用 highestOneBit(),我们打印最高位。使用 lowestOneBit(),我们打印最低位,然后使用 numberOfLeadingZeros() 方法打印最高位 1 之前的 0 位的值。
package com.tutorialspoint; public class IntegerDemo { public static void main(String[] args) { int i = 0; System.out.println("Number = " + i); /* returns the string representation of the unsigned integer value represented by the argument in binary (base 2) */ System.out.println("Binary = " + Integer.toBinaryString(i)); // returns the number of one-bits System.out.println("Number of one bits = " + Integer.bitCount(i)); /* returns an int value with at most a single one-bit, in the position of the highest-order ("leftmost") one-bit in the specified int value */ System.out.println("Highest one bit = " + Integer.highestOneBit(i)); /* returns an int value with at most a single one-bit, in the position of the lowest-order ("rightmost") one-bit in the specified int value.*/ System.out.println("Lowest one bit = " + Integer.lowestOneBit(i)); /*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(i)); } }
输出
让我们编译并运行以上程序,这将产生以下结果:
Number = 0 Binary = 0 Number of one bits = 0 Highest one bit = 0 Lowest one bit = 0 Number of leading zeros = 32