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


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。