当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Integer highestOneBit()用法及代码示例


java.lang的Integer.highestOneBit()方法返回一个整数值,该整数值最多具有单个one-bit,对于给定的int值,其位于highest-order(即最左侧)one-bit的位置。如果指定的值在其补码二进制表示形式中不包含one-bits,则其返回零(简单地等于0)。

用法:

public static int highestOneBit(int a)

参数:该方法采用整数类型的一个参数a,该参数表示要返回其最高顺序位或对其执行操作的值。


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

  • 在指定值的highest-order one-bit的位置中返回一个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 praogram 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)); 
    } 
}
输出:
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 praogram 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 praogram 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:对于字符串。
注意:由于类型不兼容而将字符串作为参数传递时,它将返回错误消息。

// Java praogram 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


相关用法


注:本文由纯净天空筛选整理自ankita_chowrasia大神的英文原创作品 Integer highestOneBit() Method in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。