Java.lang.Integer.highestOneBit() 方法
描述
java.lang.Integer.highestOneBit() 方法返回一个 int 值,在指定的最高位("最左边")一位的位置上最多只有一位 整数值。 如果指定值在其二进制补码表示中没有一位,即等于零,则返回零。
声明
以下是 java.lang.Integer.highestOneBit() 方法的声明。
public static int highestOneBit(int i)
参数
i − 这是 int 值。
返回值
此方法返回一个具有单个一位的 int 值,位于指定值中最高位的位置,如果指定值本身等于 0,则返回零。
异常
NA
示例
下面的例子展示了 java.lang.Integer.highestOneBit() 方法的使用。
package com.tutorialspoint;
import java.lang.*;
public class IntegerDemo {
public static void main(String[] args) {
int i = 170;
System.out.println("Number = " + i);
/* returns the string representation of the unsigned integer value
represented by the argument in binary (base 2) */
System.out.println("Binary = " + Integer.toBinaryString(i));
// returns the number of one-bits
System.out.println("Number of one bits = " + Integer.bitCount(i));
/* returns an int 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 = " + Integer.highestOneBit(i));
}
}
让我们编译并运行上面的程序,这将产生下面的结果 −
Number = 170 Binary = 10101010 Number of one bits = 4 Highest one bit = 128

