Java - Long lowestOneBit() 方法



描述

Java Long lowestOneBit() 方法返回一個長整型值,該值最多隻有一個位元位為 1,位於指定長整型值最低位(最右邊)的 1 位所在位置。如果指定值在其二進位制補碼錶示中沒有 1 位,即等於零,則返回零。

宣告

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

public static long lowestOneBit(long i)

引數

i − 這是一個長整型值。

返回值

此方法返回一個長整型值,其中只有一個位元位為 1,位於指定值中最低位的 1 位所在位置;如果指定值本身等於零,則返回零。

異常

從正值的長整型中獲取最低位最多隻有一個 1 的長整型值示例

以下示例演示瞭如何使用 Long lowestOneBit() 方法獲取一個長整型值,該值在最低位最多隻有一個位元位為 1。我們建立了一個長整型變數併為其賦值一個正的長整型值。然後使用 toBinaryString() 方法列印值的二進位制格式。使用 bitCount() 列印 1 位計數,然後使用 lowestOneBit() 方法列印最低位 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 lowest-order ("rightmost") one-bit in the specified long value */ 
      System.out.println("Lowest one bit = " + Long.lowestOneBit(i));
   }
}

輸出

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

Number = 170
Binary = 10101010
Number of one bits = 4
Lowest one bit = 2

從負值的長整型中獲取最低位最多隻有一個 1 的長整型值示例

以下示例演示瞭如何使用 Long lowestOneBit() 方法獲取一個長整型值,該值在最低位最多隻有一個位元位為 1。我們建立了一個長整型變數併為其賦值一個負的長整型值。然後使用 toBinaryString() 方法列印值的二進位制格式。使用 bitCount() 列印 1 位計數,然後使用 lowestOneBit() 方法列印最低位 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 lowest-order ("rightmost") one-bit in the specified long value */ 
      System.out.println("Lowest one bit = " + Long.lowestOneBit(i));
   }
}

輸出

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

Number = -170
Binary = 1111111111111111111111111111111111111111111111111111111101010110
Number of one bits = 60
Lowest one bit = 2

從值為零的長整型中獲取最低位最多隻有一個 1 的長整型值示例

以下示例演示瞭如何使用 Long lowestOneBit() 方法獲取一個長整型值,該值在最低位最多隻有一個位元位為 1。我們建立了一個長整型變數併為其賦值一個正的長整型值。然後使用 toBinaryString() 方法列印值的二進位制格式。使用 bitCount() 列印 1 位計數,然後使用 lowestOneBit() 方法列印最低位 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 lowest-order ("rightmost") one-bit in the specified long value */ 
      System.out.println("Lowest one bit = " + Long.lowestOneBit(i));
   }
}

輸出

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

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