Java - Long highestOneBit() 方法



描述

Java Long highestOneBit() 方法返回一个 long 值,该值最多只有一个 1 位,位于指定 long 值中最高位(“最左侧”)的 1 位的位置。如果指定值在其二进制补码表示形式中没有 1 位,即如果它等于零,则返回零。

声明

以下是 java.lang.Long.highestOneBit() 方法的声明

public static long highestOneBit(long i)

参数

i - 这是 long 值。

返回值

此方法返回一个 long 值,该值只有一个 1 位,位于指定值中最高位的 1 位的位置,如果指定值本身等于零,则返回零。

异常

从具有正值的 Long 获取最高位中最多只有一个 1 位的 long 值示例

以下示例演示了 Long highestOneBit() 方法的使用,以获取一个 long 值,该值在最高位的位置最多只有一个 1 位。我们创建了一个 long 变量并为其分配了一个正 long 值。然后使用 toBinaryString() 方法,我们打印值的二进制格式。使用 bitCount(),我们打印 1 位的计数,然后使用 highestOneBit() 方法打印最高 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));
   }
}

输出

让我们编译并运行上述程序,这将产生以下结果:

Number = 170
Binary = 10101010
Number of one bits = 4
Highest one bit = 128

从具有负值的 Long 获取最高位中最多只有一个 1 位的 long 值示例

以下示例演示了 Long highestOneBit() 方法的使用,以获取一个 long 值,该值在最高位的位置最多只有一个 1 位。我们创建了一个 long 变量并为其分配了一个负 long 值。然后使用 toBinaryString() 方法,我们打印值的二进制格式。使用 bitCount(),我们打印 1 位的计数,然后使用 highestOneBit() 方法打印最高 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));
   }
}

输出

让我们编译并运行上述程序,这将产生以下结果:

Number = -170
Binary = 1111111111111111111111111111111111111111111111111111111101010110
Number of one bits = 60
Highest one bit = -9223372036854775808

从具有零值的 Long 获取最高位中最多只有一个 1 位的 long 值示例

以下示例演示了 Long highestOneBit() 方法的使用,以获取一个 long 值,该值在最高位的位置最多只有一个 1 位。我们创建了一个 long 变量并为其分配了一个正 long 值。然后使用 toBinaryString() 方法,我们打印值的二进制格式。使用 bitCount(),我们打印 1 位的计数,然后使用 highestOneBit() 方法打印最高 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));
   }
}

输出

让我们编译并运行上述程序,这将产生以下结果:

Number = 0
Binary = 0
Number of one bits = 0
Highest one bit = 0
java_lang_long.htm
广告

© . All rights reserved.