import java.util.BitSet; public class BitSetExample { public static void main(String[] args) { // create a new BitSet BitSet set = new BitSet(8); // set some bits set.set(0); set.set(2); set.set(5); // clone the BitSet BitSet clone = (BitSet) set.clone(); // output the two bitsets System.out.println("Original: " + set); System.out.println("Clone: " + clone); // modify the clone clone.clear(0); clone.set(3); // output the two bitsets again System.out.println("Original: " + set); System.out.println("Modified Clone: " + clone); } }
import java.util.BitSet; public class BitSetExample { public static void main(String[] args) { // create a new BitSet BitSet set = new BitSet(8); // set some bits set.set(0); set.set(2); set.set(5); // clone the BitSet BitSet clone = (BitSet) set.clone(); // use the clone in a loop for (int i = clone.nextSetBit(0); i >= 0; i = clone.nextSetBit(i+1)) { System.out.println("Clone contains bit " + i); } } }This example creates a new BitSet and sets some bits. It then creates a clone of the BitSet and uses it in a loop to find the next set bit. The loop will continue until there are no more set bits in the cloned BitSet. The java.util.BitSet class is part of the Java standard library, which comes installed with the Java Development Kit (JDK). It is located in the java.util package.