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


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


java.lang的Integer.lowestOneBit()方法是一个内置函数,该函数返回一个int值,该int值在指定int值中最低(即最右)的one-bit的位置处最多包含一个one-bit。如果指定值在其补码二进制表示形式中没有one-bits,则此方法将返回零。如果数字的二进制表示等于零。

用法:

public static int lowestOneBit(int a)

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


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

  • 在指定值的最低顺序one-bit的位置中返回一个1位整数值
  • 如果指定值等于零,则返回零。

例子:

Input:157
Output:Lowest one bit = 1

Input:0
Output:Lowest one bit = 0

Explanation:
Consider any integer a = 10
Binary Representation = 0000 1010
Lowest bit(at 1) i.e.0000 0010
so result = 2^1=2

以下程序说明了java.lang.Integer.lowestOneBit()方法:

程序1:为正数。

// Java praogram to illustrate the 
// java.lang.Integer.lowestOneBit() method 
import java.lang.*; 
  
public class Geeks { 
  
    public static void main(String[] args) 
    { 
  
        int a = 157; 
        System.out.println("Given Number = " + a); 
  
        // Returns an int value with at most a single one-bit 
        System.out.print("Lowest one bit = "); 
        System.out.println(Integer.lowestOneBit(a)); 
  
        a = 64; 
        System.out.println("Given Number = " + a); 
  
        System.out.print("Lowest one bit = "); 
        System.out.println(Integer.lowestOneBit(a)); 
  
        // Here it will return 0 since the number is itself zero 
        a = 0; 
        System.out.println("Given Number = " + a); 
  
        System.out.print("Lowest one bit = "); 
        System.out.println(Integer.lowestOneBit(a)); 
    } 
}
输出:
Given Number = 157
Lowest one bit = 1
Given Number = 64
Lowest one bit = 64
Given Number = 0
Lowest one bit = 0

程序2:为负数。

// Java praogram to illustrate the 
// java.lang.Integer.lowestOneBit() method 
import java.lang.*; 
  
public class Geeks { 
  
    public static void main(String[] args) 
    { 
  
        int a = -157; 
        System.out.println("Given Number = " + a); 
  
        // It will return an int value with at most a single one-bit 
        System.out.print("Lowest one bit = "); 
        System.out.println(Integer.lowestOneBit(a)); 
        a = -17; 
        System.out.println("Given Number = " + a); 
  
        System.out.print("Lowest one bit = "); 
        System.out.println(Integer.lowestOneBit(a)); 
    } 
}
输出:
Given Number = -157
Lowest one bit = 1
Given Number = -17
Lowest one bit = 1

程序3:对于十进制值和字符串。
注意:当将十进制值和字符串作为参数传递时,它将返回编译时错误消息。

// Java praogram to illustrate the 
// java.lang.Integer.lowestOneBit() method 
import java.lang.*; 
  
public class Geeks { 
  
    public static void main(String[] args) 
    { 
  
        // Decimal value is given 
        int a = 71.57; 
        System.out.println("Given Number = " + a); 
  
        System.out.print("Lowest one bit = "); 
        System.out.println(Integer.lowestOneBit(a)); 
  
        // String is passed 
        a = "12"; 
        System.out.println("Given Number = " + a); 
  
        System.out.print("Lowest one bit = "); 
        System.out.println(Integer.lowestOneBit(a)); 
    } 
}

输出:

prog.java:10:error:incompatible types:possible lossy conversion from double to int
    int a = 71.57;
            ^
prog.java:17:error:incompatible types:String cannot be converted to int
    a = "12";
        ^
2 errors


相关用法


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