📜  Java中的整数highestOneBit()方法

📅  最后修改于: 2022-05-13 01:54:49.859000             🧑  作者: Mango

Java中的整数highestOneBit()方法

Java.lang 的 Integer.highestOneBit() 方法返回一个整数值,该整数值最多只有一个一位,该整数值位于给定 int 值的最高位(即最左边)一位的位置。如果指定的值在其二进制补码二进制表示中没有一位,则它返回零,只要它等于零。
句法:

public static int highestOneBit(int a)

参数:该方法采用一个整数类型的参数a ,该参数是指要返回其最高位或要对其执行操作的值。
Returns :该方法可以返回两种类型的值:

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

例子:

Input: 19
Output: Highest one bit of the given integer is = 16

Input: -9
Output: Highest one bit of the given integer is = -2147483648

Explanation:
Consider any integer a = 19
Binary Representation = 0001 0011
Highest bit(at 4) i.e.0001 0000
so result = 2^4=16

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

java
// Java program to illustrate the
// Java.lang.Integer.highestOneBit() Method
import 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));
    }
}


java
// Java program to illustrate the
// Java.lang.Integer.highestOneBit() Method
import 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));
    }
}


java
// Java program to illustrate the
// Java.lang.Integer.highestOneBit() Method
import 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));
    }
}


java
// Java program to illustrate the
// Java.lang.Integer.highestOneBit() Method
import 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));
    }
}


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

程序 2:对于负数。

Java

// Java program to illustrate the
// Java.lang.Integer.highestOneBit() Method
import 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 = -9
Highest one bit of the given integer is = -2147483648

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

Java

// Java program to illustrate the
// Java.lang.Integer.highestOneBit() Method
import 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:对于字符串。
注意:当由于类型不兼容而将 String 作为参数传递时,它会返回错误消息。

Java

// Java program to illustrate the
// Java.lang.Integer.highestOneBit() Method
import 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