- 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 - Long numberOfLeadingZeros() 方法
描述
Java Long numberOfLeadingZeros() 方法返回指定 long 值的二進位制補碼錶示中最高位(“最左邊”)的 1 位之前的 0 位數。
如果指定值在其二進位制補碼錶示中沒有 1 位,換句話說,如果它等於零,則返回 32。
宣告
以下是 java.lang.Long.numberOfLeadingZeros() 方法的宣告
public static int numberOfLeadingZeros(long i)
引數
i − 這是 long 值。
返回值
此方法返回指定 long 值的二進位制補碼錶示中最高位(“最左邊”)的 1 位之前的 0 位數,如果該值為零,則返回 32。
異常
無
從具有正值的 Long 獲取最高位 1 位之前的 0 位數示例
以下示例演示瞭如何使用 Long numberOfLeadingZeros() 方法獲取最高位 1 位之前的 0 位數。我們建立了一個 long 變數併為其分配了一個正 long 值。然後使用 toBinaryString() 方法,我們列印該值的二進位制格式。使用 bitCount(),我們列印 1 位計數。使用 highestOneBit(),我們列印了最高位。使用 lowestOneBit(),我們列印了最低位,然後使用 numberOfLeadingZeros() 方法列印了最高位 1 位之前的 0 位數的值。
package com.tutorialspoint;
public class LongDemo {
public static void main(String[] args) {
long i = 170L;
System.out.println("Number = " + i);
/* returns the string representation of the unsigned long value
represented by the argument in binary (base 2) */
System.out.println("Binary = " + Long.toBinaryString(i));
// returns the number of one-bits
System.out.println("Number of one bits = " + Long.bitCount(i));
/* returns an long value with at most a single one-bit, in the position
of the highest-order ("leftmost") one-bit in the specified long value */
System.out.println("Highest one bit = " + Long.highestOneBit(i));
/* returns an long value with at most a single one-bit, in the position
of the lowest-order ("rightmost") one-bit in the specified long value.*/
System.out.println("Lowest one bit = " + Long.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(Long.numberOfLeadingZeros(i));
}
}
輸出
讓我們編譯並執行以上程式,這將產生以下結果:
Number = 170 Binary = 10101010 Number of one bits = 4 Highest one bit = 128 Lowest one bit = 2 Number of leading zeros = 56
從具有負值的 Long 獲取最高位 1 位之前的 0 位數示例
以下示例演示瞭如何使用 Long numberOfLeadingZeros() 方法獲取最高位 1 位之前的 0 位數。我們建立了一個 long 變數併為其分配了一個負 long 值。然後使用 toBinaryString() 方法,我們列印該值的二進位制格式。使用 bitCount(),我們列印 1 位計數。使用 highestOneBit(),我們列印了最高位。使用 lowestOneBit(),我們列印了最低位,然後使用 numberOfLeadingZeros() 方法列印了最高位 1 位之前的 0 位數的值。
package com.tutorialspoint;
public class LongDemo {
public static void main(String[] args) {
long i = -170L;
System.out.println("Number = " + i);
/* returns the string representation of the unsigned long value
represented by the argument in binary (base 2) */
System.out.println("Binary = " + Long.toBinaryString(i));
// returns the number of one-bits
System.out.println("Number of one bits = " + Long.bitCount(i));
/* returns an long value with at most a single one-bit, in the position
of the highest-order ("leftmost") one-bit in the specified long value */
System.out.println("Highest one bit = " + Long.highestOneBit(i));
/* returns an long value with at most a single one-bit, in the position
of the lowest-order ("rightmost") one-bit in the specified long value.*/
System.out.println("Lowest one bit = " + Long.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(Long.numberOfLeadingZeros(i));
}
}
輸出
讓我們編譯並執行以上程式,這將產生以下結果:
Number = -170 Binary = 1111111111111111111111111111111111111111111111111111111101010110 Number of one bits = 60 Highest one bit = -9223372036854775808 Lowest one bit = 2 Number of leading zeros = 0
從具有零值的 Long 獲取最高位 1 位之前的 0 位數示例
以下示例演示瞭如何使用 Long numberOfLeadingZeros() 方法獲取最高位 1 位之前的 0 位數。我們建立了一個 long 變數併為其分配了一個零 long 值。然後使用 toBinaryString() 方法,我們列印該值的二進位制格式。使用 bitCount(),我們列印 1 位計數。使用 highestOneBit(),我們列印了最高位。使用 lowestOneBit(),我們列印了最低位,然後使用 numberOfLeadingZeros() 方法列印了最高位 1 位之前的 0 位數的值。
package com.tutorialspoint;
public class LongDemo {
public static void main(String[] args) {
long i = 0L;
System.out.println("Number = " + i);
/* returns the string representation of the unsigned long value
represented by the argument in binary (base 2) */
System.out.println("Binary = " + Long.toBinaryString(i));
// returns the number of one-bits
System.out.println("Number of one bits = " + Long.bitCount(i));
/* returns an long value with at most a single one-bit, in the position
of the highest-order ("leftmost") one-bit in the specified long value */
System.out.println("Highest one bit = " + Long.highestOneBit(i));
/* returns an long value with at most a single one-bit, in the position
of the lowest-order ("rightmost") one-bit in the specified long value.*/
System.out.println("Lowest one bit = " + Long.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(Long.numberOfLeadingZeros(i));
}
}
輸出
讓我們編譯並執行以上程式,這將產生以下結果:
Number = 0 Binary = 0 Number of one bits = 0 Highest one bit = 0 Lowest one bit = 0 Number of leading zeros = 64