BitSet previousSetBit() method in Java

BitSet is a class defined in the java.util package. It creates an array of bits represented by boolean values.

Prerequisite : Java BitSet | Set 1

Bitset.previousSetBit()
This method is used to find whether there are any true bits that occur on or before the specified starting index.
This function returns the index of the nearest bit that is set to true that occurs on or before the specified starting index. If no such bit exists, or if -1 is given as the starting index, then -1 is returned.

Syntax:

public int previousSetBit(int fromIndex)

Parameters: This method takes a mandatory parameter fromIndex which is the index to start checking from (inclusive) for the true bit that occur on or before this fromIndex.

Return Value: This method returns the index of the previous set bit that occurs on or before the specified index, or -1 if there is no such bit.

Exception: This method throws IndexOutOfBoundsException if the specified index is less than -1.

Note :To iterate over the true bits in a BitSet, use the following loop:

for (int i = bs.length(); (i = bs.previousSetBit(i-1)) >= 0; ) {
     // operate on index i here
 }

Below programs illustrate the previousSetBit() method:

Example 1: To show implementation of previousSetBit() function :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program illustrating Bitset
// previousSetBit() function.
  
import java.util.*;
public class GFG {
    public static void main(String[] args)
    {
        // Constructors of BitSet class
        BitSet bs1 = new BitSet();
        BitSet bs2 = new BitSet();
        BitSet bs3 = new BitSet();
  
        /* assigning values to set1*/
        bs1.set(0);
        bs1.set(1);
        bs1.set(2);
        bs1.set(4);
  
        // assign values to bs2
        bs2.set(4);
        bs2.set(6);
        bs2.set(5);
        bs2.set(1);
        bs2.set(2);
        bs2.set(3);
        bs2.set(12);
  
        // Printing the 2 Bitsets
        System.out.println("bs1 : " + bs1);
        System.out.println("bs2 : " + bs2);
        System.out.println("bs3 : " + bs3);
  
        // Performing length() on bitsets
        System.out.println("Previous Set Bit of bs1 "
                           + bs1.previousSetBit(2));
        System.out.println("Previous Set Bit of bs2 "
                           + bs2.previousSetBit(1));
        System.out.println("Previous Set Bit of bs3 "
                           + bs3.previousSetBit(3));
    }
}

chevron_right


Output:

bs1 : {0, 1, 2, 4}
bs2 : {1, 2, 3, 4, 5, 6, 12}
bs3 : {}
Previous Set Bit of bs1 2
Previous Set Bit of bs2 1
Previous Set Bit of bs3 -1

Example 2: To show IndexOutOfBoundException:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program illustrating Bitset
// previousSetBit() function.
  
import java.util.*;
public class GFG {
    public static void main(String[] args)
    {
        // Constructors of BitSet class
        BitSet bs1 = new BitSet();
  
        // assigning values to set1
        bs1.set(0);
        bs1.set(1);
        bs1.set(2);
        bs1.set(4);
  
        // Printing the Bitset
        System.out.println("bs1 : " + bs1);
  
        try {
            // Passing -2 as parameter
            System.out.println(bs1.previousSetBit(-2));
        }
        catch (Exception e) {
            System.out.println("Exception when "
                               + "index less than -1 is passed "
                               + "as parameter : " + e);
        }
    }
}

chevron_right


Output:

bs1 : {0, 1, 2, 4}
Exception when index less than -1 is passed as parameter : java.lang.IndexOutOfBoundsException: fromIndex < -1: -2


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.