當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。