Sunday, November 17, 2024
Google search engine
HomeLanguagesJavaBitSet previousClearBit() method in Java

BitSet previousClearBit() 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.previousClearBit()
This method is used to find the false bit that occur on or before the index passed as the parameter. This method returns the index of the nearest bit that is set to false 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 previousClearBit(int fromIndex)

Parameters: This method takes fromIndex which is the index to start checking from (inclusive).

Return Value: This method returns the index of the previous clear bit, or -1 if there is no such bit

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

Below programs illustrate the previousCleatBit() method:

Program 1:




// Java program illustrating Bitset
// previousClearBit() 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 previousClearBit() on bitsets
        System.out.println("Previous Clear Bit of bs1"
+ bs1.previousClearBit(0));
        System.out.println("Previous Clear Bit of bs2"
+ bs2.previousClearBit(5));
        System.out.println("Previous Clear Bit of bs3"
+ bs3.previousClearBit(3));
          
    }


Output:

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

Program 2: To show IndexOutOfBoundException:




// Java program illustrating Bitset
// previousClearBit() 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.previousClearBit(-2));
        }
        catch (Exception e) {
            System.out.println("Exception when "
                               + "index less than -1 is passed "
                               + "as parameter : " + e);
        }
    }
}


Output:

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

Most Popular

Recent Comments