ADVERTISEMENT
ADVERTISEMENT

Java BigInteger Class | getLowestSetBit() Method with Example

BigInteger Class getLowestSetBit() method: Here, we are going to learn about the getLowestSetBit() method of BigInteger Class with its syntax and example.
Submitted by Preeti Jain, on May 10, 2020

BigInteger Class getLowestSetBit() method

  • getLowestSetBit() method is available in java.math package.
  • getLowestSetBit() method is used to returns the index of 1's bit from the rightmost side in this BigInteger.
  • getLowestSetBit() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • getLowestSetBit() method does not throw an exception at the time of returning the lowest set bit.

Syntax:

    public int getLowestSetBit();

Parameter(s):

  • None

Return value:

The return type of this method is int, it returns the position of first 1’s bit from the rightmost side in this BigInteger.

Example:

// Java program to demonstrate the example 
// of int getLowestSetBit() method of BigInteger

import java.math.*;

public class GetLowestSetBitOfBI {
    public static void main(String args[]) {
        // Initialize two variables str1 and str2
        String str1 = "10";
        String str2 = "5";

        // Initialize two BigInteger object 
        BigInteger b_int1 = new BigInteger(str1);
        BigInteger b_int2 = new BigInteger(str2);

        // Display b_int1 and b_int2
        System.out.println("b_int1: " + b_int1);
        System.out.println("b_int2: " + b_int2);

        System.out.println();

        // Display Binary representation of 
        // str1 and str2 
        System.out.println("Binary Representation of 10: 1010 ");
        System.out.println("Binary Representation of 5: 0101 ");

        System.out.println();

        // returns the index to the right of the
        // rightmost "1" bit set so the binary representation
        // of 10 is 1010 and in that bit "1" is indexed at 
        // index 1 and 3 from the right side then the index of first
        // occurrence of "1" will be returned

        int lowest_bit = b_int1.getLowestSetBit();
        System.out.println("b_int1.getLowestSetBit(): " + lowest_bit);

        // returns the index to the right of the
        // rightmost "1" bit set so the binary representation
        // of 5 is 0101 and in that bit "1" is indexed at 
        // index 0 and 2 from the right side then the index of first
        // occurrence of "1" will be returned
        lowest_bit = b_int2.getLowestSetBit();
        System.out.println("b_int2.getLowestSetBit(): " + lowest_bit);
    }
}

Output

b_int1: 10
b_int2: 5

Binary Representation of 10: 1010 
Binary Representation of 5: 0101 

b_int1.getLowestSetBit(): 1
b_int2.getLowestSetBit(): 0
ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.