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.