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


Java BigInteger getLowestSetBit()用法及代碼示例


先決條件:BigInteger基礎

java.math.BigInteger.getLowestSetBit()方法返回此BigInteger的最右邊(lowest-order)設置位的索引。這意味著此方法返回最右邊的設置位右邊的零或未設置的位的數目。如果BigInteger不包含任何設置位,則此方法將返回-1。該方法計算(thisBigInteger == 0?-1:log2(thisBigInteger&-thisBigInteger))。

用法:


public int getLowestSetBit()

參數:該方法不接受任何參數。

返回值:該方法返回此BigInteger中最右邊的設置位的索引。

例子:

Input: value = 2300 
Output: 2
Explanation:
Binary Representation of 2300 = 100011111100
The lowest set bit index is 2

Input: value = 35000 
Output: 3

以下示例程序旨在說明BigInteger的getLowestSetBit()方法:

// Program to illustrate the getLowestSetBit() 
// method of BigInteger  
  
import java.math.*; 
  
public class GFG { 
  
    public static void main(String[] args) 
    { 
  
        // Create BigInteger object 
        BigInteger biginteger = new BigInteger("2300"); 
  
        // Call getLowestSetBit() method on bigInteger 
        // Store the return value as Integer lowestsetbit 
        int lowestSetbit = biginteger.getLowestSetBit(); 
  
        String lsb = "After applying getLowestSetBit on " + biginteger + 
                       " we get index of lowest set bit = " + lowestSetbit; 
  
        // Printing result 
        System.out.println(lsb); 
    } 
}
輸出:
After applying getLowestSetBit on 2300 we get index of lowest set bit = 2

參考: https://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html#getLowestSetBit()



相關用法


注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 BigInteger getLowestSetBit() Method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。