Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator

What is BitSet.nextSetBit() in Java?

abhilash

nextSetBit() is an instance method of the BitSet class that is used to get the index of the first bit that is set to true on or after the given beginning index. The method returns -1 if no bit is set.

The nextSetBit 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 nextSetBit(int fromIndex)

Parameters

int fromIndex: The starting index.

Return value

This method returns the index of the next set 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
        bitSet.set(2);

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

        int startingIndex = 4;

        // get the first bit set to true from the startingIndex
        System.out.printf("%s.nextSetBit(%s) = %s", bitSet, startingIndex, bitSet.nextSetBit(startingIndex));
    }
}

RELATED TAGS

java
communitycreator
RELATED COURSES

View all Courses

Keep Exploring