BitSet nextSetBit() 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
nextSetBit() method :
This method in BitSet Class is used to return the index of the first bit that is set to true, that occurs on or after the specified starting index. If no such bit exists then -1 is returned.
Syntax:
public int nextSetBit(int fromIndex)
Parameters: This method takes a mandatory parameter fromIndex which is the index to start checking from (inclusive) for the next true bit.
Return Value: This method returns the index of the next set bit, or -1 if there is no such bit
Exception: This method throws IndexOutOfBoundsException if the specified index is negative.
Note : To iterate over the true bits in a BitSet, use the following loop:
for (int i = bs.nextSetBit(0); i >= 0; i = bs.nextSetBit(i+1)) { // operate on index i here }
Example 1: To show the implementation of nextSetBit() function :
// Java program illustrating Bitset // nextSetBit() 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 nextSetBit() on bitsets System.out.println(bs1.nextSetBit( 2 )); System.out.println(bs2.nextSetBit( 0 )); System.out.println(bs3.nextSetBit( 3 )); } } |
bs1 : {0, 1, 2, 4} bs2 : {1, 2, 3, 4, 5, 6, 12} bs3 : {} 2 1 -1
Example 2: To show IndexOutOfBoundException:
// Java program illustrating Bitset // nextSetBit() 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(); /* 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); try { // Passing -1 as parameter System.out.println(bs1.nextSetBit(- 1 )); } catch (Exception e) { System.out.println( "Exception when " + "negative index is passed " + "as parameter : " + e); } } } |
bs1 : {0, 1, 2, 4} bs2 : {1, 2, 3, 4, 5, 6, 12} Exception when negative index is passed as parameter : java.lang.IndexOutOfBoundsException: fromIndex < 0: -1
Recommended Posts:
- BitSet previousClearBit() method in Java
- BitSet nextClearBit() method in Java
- BitSet previousSetBit() method in Java
- BitSet isEmpty() method in Java
- BitSet toLongArray() Method in Java with Examples
- BitSet toByteArray() Method in Java with Examples
- BitSet clone() Method in Java with Examples
- BitSet size() Method in Java with Examples
- BitSet hashCode Method in Java with Examples
- BitSet stream() Method in Java with Examples
- BitSet equals() Method in Java with Examples
- BitSet length() Method in Java with Examples
- BitSet toString() Method in Java with Examples
- Java.util.BitSet.set() method in Java
- Java.util.BitSet class methods in Java with Examples | Set 2
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.