Java Integer.estimOneBit()方法

来源:这里教程网 时间:2026-02-17 20:51:12 作者:

Java Integer.estimOneBit()方法

java.lang的Integer.estimOneBit()方法返回一个整数值,该整数值最多只有一个位,位于给定int值的最高阶(即最左边)的一个位的位置。如果指定的值在其二进制补码表示中没有一比特,那么它将返回0,如果它等于0,则简单地返回。

语法

public static int highestOneBit( _int a_ )

参数: 该方法需要一个整数类型的参数 a ,指的是要返回的最高阶位的值或要对其进行的操作。

返回: 该方法可以返回两种类型的值。

返回一个带有单个1位的整数值,位于指定值中最高阶1位的位置。如果指定的值等于零,则返回零。

例子

Input: 19Output: Highest one bit of the given integer is = 16Input: -9Output: Highest one bit of the given integer is = -2147483648**Explanation:**Consider any integer a = 19Binary Representation = 0001 0011Highest bit(at 4) i.e.0001 0000so result = 2^4=16

下面的程序说明了Java.lang.Integer.highestOneBit()方法。
程序1: 对于一个正数。

// Java program to illustrate the// Java.lang.Integer.highestOneBit() Methodimport java.lang.*; public class HighestOneBit {     public static void main(String[] args)    {         int a = 29;        System.out.println("Number is = " + a);         // returns an int value with at most a single one-bit, in the position        // of the highest-order or the leftmost one-bit in the specified int value        System.out.print("Highest one bit of the given integer is = ");        System.out.println(Integer.highestOneBit(a));        a = 44;        System.out.println("Number is = " + a);        System.out.print("Highest one bit of the given integer is = ");        System.out.println(Integer.highestOneBit(a));    }}

输出

Number is = 29Highest one bit of the given integer is = 16Number is = 44Highest one bit of the given integer is = 32

程序2: 对于一个负数。

// Java program to illustrate the// Java.lang.Integer.highestOneBit() Methodimport java.lang.*; public class HighestOneBit {     public static void main(String[] args)    {         int a = -9;        System.out.println("Number is = " + a);         // returns an int value with at most a single one-bit, in the position        // of the highest-order or the leftmost one-bit in the specified int value        System.out.print("Highest one bit of the given integer is = ");        System.out.println(Integer.highestOneBit(a));    }}

输出

Number is = -9Highest one bit of the given integer is = -2147483648

程序3: 对于一个十进制数值。
注意: 由于类型不兼容,当十进制数值作为一个参数被传递时,它将返回一个错误信息。

// Java program to illustrate the// Java.lang.Integer.highestOneBit() Methodimport java.lang.*; public class HighestOneBit {     public static void main(String[] args)    {         int a = 84.22;        System.out.println("Number is = " + a);         // decimal value 84.22 is passed here        System.out.print("Highest one bit of the given integer is = ");        System.out.println(Integer.highestOneBit(a));    }}

输出

prog.java:9: error: incompatible types: possible lossy conversion from double to int    int a = 84.22;            ^1 error

程序4: 对于一个字符串。
注意: 当一个字符串作为参数被传递时,由于类型不兼容,它将返回一个错误信息。

// Java program to illustrate the// Java.lang.Integer.highestOneBit() Methodimport java.lang.*; public class HighestOneBit {     public static void main(String[] args)    {         int a = "34";        System.out.println("Number is = " + a);         // decimal value 84.22 is passed here        System.out.print("Highest one bit of the given integer is = ");        System.out.println(Integer.highestOneBit(a));    }}

输出

prog.java:9: error: incompatible types: String cannot be converted to int    int a = "34";            ^1 error

相关推荐