Java.lang.Long.highestOneBit() 方法
描述
java.lang.Long.highestOneBit() 方法返回一个 long 值,在指定的最高位("最左边")一位的位置上最多只有一位 长期价值。 如果指定值在其二进制补码表示中没有一位,即等于零,则返回零。
声明
以下是 java.lang.Long.highestOneBit() 方法的声明。
public static long highestOneBit(long i)
参数
i − 这是 long 值。
返回值
此方法返回一个具有单个一位的长值,位于指定值中最高位的位置,如果指定值本身等于零,则返回零。
异常
NA
示例
下面的例子展示了 java.lang.Long.highestOneBit() 方法的使用。
package com.tutorialspoint;
import java.lang.*;
public class LongDemo {
public static void main(String[] args) {
long l = 220;
System.out.println("Number = " + l);
/* returns the string representation of the unsigned long value
represented by the argument in binary (base 2) */
System.out.println("Binary = " + Long.toBinaryString(l));
// returns the number of one-bits
System.out.println("Number of one bits = " + Long.bitCount(l));
/* returns a long value with at most a single one-bit, in the position
of the highest-order ("leftmost") one-bit in the specified int value */
System.out.println("Highest one bit = " + Long.highestOneBit(l));
}
}
让我们编译并运行上面的程序,这将产生下面的结果 −
Number = 220 Binary = 11011100 Number of one bits = 5 Highest one bit = 128

