Java.math.BigInteger.getLowestSetBit() Method

Advertisements

Description

The java.math.BigInteger.getLowestSetBit() returns the index of the rightmost (lowest-order) one bit in this BigInteger (the number of zero bits to the right of the rightmost one bit). It returns -1 if this BigInteger contains no one bits. It computes (this==0? -1 : log2(this & -this)).

Declaration

Following is the declaration for java.math.BigInteger.getLowestSetBit() method.

public int getLowestSetBit()

NA

Return Value

This method returns the index of the rightmost one bit in this BigInteger.

NA

Example

The following example shows the usage of math.BigInteger.getLowestSetBit() method.

package com.tutorialspoint;

import java.math.*;

public class BigIntegerDemo {

public static void main(String[] args) {

// create 2 BigInteger objects
BigInteger bi1, bi2;

// create 2 int objects
int i1, i2;

// assign values to bi1, bi2
bi1 = new BigInteger("8");//1000
bi2 = new BigInteger("7");//0111

// perform getLowestSetBit on bi1, bi2
i1 = bi1.getLowestSetBit();
i2 = bi2.getLowestSetBit();

String str1 = "Index of rightmost one bit in " +bi1+ " is " +i1;
String str2 = "Index of rightmost one bit in " +bi2+ " is " +i2;

// print i1, i2 values
System.out.println( str1 );
System.out.println( str2 );
}
}

Let us compile and run the above program, this will produce the following result −

Index of rightmost one bit in 8 is 3
Index of rightmost one bit in 7 is 0

java_math_biginteger.htm

Advertisements