• Java Data Structures Tutorial

Java Data Structures - Creating a Bitset



You can create a BitSet by instantiating the BitSet class of the java.util package. One of the constructor of the BitSet class allows you to specify its initial size, i.e., the number of bits that it can hold.

Therefore, to create a bit set instantiate the BitSet class by passing the required number of bits to its constructor.

BitSet bitSet = new BitSet(5);

Example

import java.util.BitSet;
public class CreatingBitSet {
   public static void main(String args[]) {
      
      BitSet bitSet = new BitSet(5);   
      bitSet.set(0);
      bitSet.set(2);
      bitSet.set(4);
      System.out.println(bitSet);      
   }
}

Output

{0, 2, 4}
Advertisements