static int highestOneBit(int i)
描述 (Description)
java.lang.Integer.highestOneBit()方法返回一个int值,该值最多只有一位,位于指定int值中最高位(“最左侧”)一位的位置。 如果指定的值在其二进制补码表示中没有一位,即它等于零,则返回零。
声明 (Declaration)
以下是java.lang.Integer.highestOneBit()方法的声明
public static int highestOneBit(int i)
参数 (Parameters)
i - 这是int值。
返回值 (Return Value)
此方法返回一个int值,该值在指定值的最高位一位的位置使用单个位,如果指定值本身等于零,则返回零。
异常 (Exception)
NA
例子 (Example)
以下示例显示了java.lang.Integer.highestOneBit()方法的用法。
package com.iowiki;
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