Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator
previousclearbit
bitset

What is BitSet.previousClearBit() in Java?

abhilash

previousClearBit() is an instance method of the BitSet class used to get the index of the first bit that is set to false on or before the given beginning index.

The previousClearBit method is defined in the BitSet class. The BitSet class is defined in the java.util package. To import the BitSet class, use the following import statement:

import java.util.BitSet;

Syntax

public int previousClearBit(int fromIndex)

Parameters

  • fromIndex: The starting index.

Return value

This method returns the index of the previous clear bit.

Code

import java.util.BitSet;

public class Main{

    public static void main(String[] args) {
        // Create empty BitSet object
        BitSet bitSet = new BitSet();

        // Set the bit at index 2 to true
        bitSet.set(2);

        // Set the bit at index 8 to true
        bitSet.set(8);

        int startingIndex = 4;

        // get the first bit set to false before the startingIndex
        System.out.printf("%s.previousClearBit(%s) = %s", bitSet, startingIndex, bitSet.previousClearBit(startingIndex));
    }
}
Using the "previousClearBit" method

Explanation

  • Line 1: We import the relevant packages.
  • Line 7: We create an empty BitSet object.
  • Line 10: We set the bit at index 2 to true.
  • Line 13: We set the bit at index 8 to true. Our BitSet object now looks like: 001000001.
  • Line 15: We define the starting index value as 4.
  • Line 18: We get the first bit set to false before the startingIndex.

RELATED TAGS

java
communitycreator
previousclearbit
bitset
RELATED COURSES

View all Courses

Keep Exploring