Java.lang.Long.numberOfTrailingZeros() 方法



描述

java.lang.Long.numberOfTrailingZeros() 方法返回指定長整數值的二進位制補碼錶示中最低位(“最右邊”)的1位之後零位的數量。如果指定值在其二進位制補碼錶示中沒有1位,換句話說,如果它等於零,則返回64。

宣告

以下是java.lang.Long.numberOfTrailingZeros() 方法的宣告

public static int numberOfTrailingZeros(long i)

引數

i − 這是長整數值。

返回值

此方法返回指定長整數值的二進位制補碼錶示中最低位(“最右邊”)的1位之後零位的數量,如果該值為零,則返回64。

異常

從正值的長整型中獲取最低位1位之後零位的數量示例

以下示例演示瞭如何使用 Long numberOfTrailingZeros() 方法獲取最低位1位之後零位的數量。我們建立了一個長整型變數併為其賦值一個正的長整數值。然後使用 toBinaryString() 方法列印值的二進位制格式。使用 bitCount() 列印1位的數量。使用 highestOneBit() 列印最高位。使用 lowestOneBit() 列印最低位,然後使用 numberOfTrailingZeros() 方法列印最低位1位之後零位的數量。

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 trailing zeros = ");
      System.out.println(Long.numberOfTrailingZeros(i));
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Number = 170
Binary = 10101010
Number of one bits = 4
Highest one bit = 128
Lowest one bit = 2
Number of trailing zeros = 1

從負值的長整型中獲取最低位1位之後零位的數量示例

以下示例演示瞭如何使用 Long numberOfTrailingZeros() 方法獲取最低位1位之後零位的數量。我們建立了一個長整型變數併為其賦值一個負的長整數值。然後使用 toBinaryString() 方法列印值的二進位制格式。使用 bitCount() 列印1位的數量。使用 highestOneBit() 列印最高位。使用 lowestOneBit() 列印最低位,然後使用 numberOfTrailingZeros() 方法列印最低位1位之後零位的數量。

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 trailing zeros = ");
      System.out.println(Long.numberOfTrailingZeros(i));
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Number = -170
Binary = 1111111111111111111111111111111111111111111111111111111101010110
Number of one bits = 60
Highest one bit = -9223372036854775808
Lowest one bit = 2
Number of trailing zeros = 1

從值為零的長整型中獲取最低位1位之後零位的數量示例

以下示例演示瞭如何使用 Long numberOfTrailingZeros() 方法獲取最低位1位之後零位的數量。我們建立了一個長整型變數併為其賦值一個值為零的長整數值。然後使用 toBinaryString() 方法列印值的二進位制格式。使用 bitCount() 列印1位的數量。使用 highestOneBit() 列印最高位。使用 lowestOneBit() 列印最低位,然後使用 numberOfTrailingZeros() 方法列印最低位1位之後零位的數量。

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 trailing zeros = ");
      System.out.println(Long.numberOfTrailingZeros(i));
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Number = 0
Binary = 0
Number of one bits = 0
Highest one bit = 0
Lowest one bit = 0
Number of trailing zeros = 64
java_lang_long.htm
廣告
© . All rights reserved.