先決條件: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()
相關用法
- Java BigInteger pow()用法及代碼示例
- Java BigInteger and()用法及代碼示例
- Java BigInteger or()用法及代碼示例
- Java BigInteger xor()用法及代碼示例
- Java BigInteger not()用法及代碼示例
- Java BigInteger mod()用法及代碼示例
- Java BigInteger abs()用法及代碼示例
- Java BigInteger remainder()用法及代碼示例
- Java BigInteger equals()用法及代碼示例
- Java BigInteger valueOf()用法及代碼示例
- Java BigInteger toByteArray()用法及代碼示例
- Java BigInteger sqrt()用法及代碼示例
- Java BigInteger doubleValue()用法及代碼示例
- Java BigInteger negate()用法及代碼示例
- Java BigInteger flipBit()用法及代碼示例
注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 BigInteger getLowestSetBit() Method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。