BitSet bs = new BitSet(8); bs.set(1); bs.set(3); bs.set(5); int nextSet = bs.nextSetBit(0); while (nextSet != -1) { System.out.println(nextSet); nextSet = bs.nextSetBit(nextSet + 1); }
1 3 5
import java.util.BitSet; public class BitSetExample { public static void main(String[] args) { BitSet bs = new BitSet(); bs.set(4); bs.set(8); bs.set(12); int nextSet = bs.nextSetBit(0); if (nextSet >= 0) { System.out.println("The next set bit is at index " + nextSet); } else { System.out.println("There are no set bits in the BitSet."); } } }
The next set bit is at index 4The package library for java.util.BitSet is java.util.