The BitSet Class

To shift gears further, java.util includes the BitSet class as a convenient way to maintain and manipulate a set of bits. A BitSet instance can grow as needed, so you can create an empty set with the no-argument constructor as a starting point:

public BitSet()

If, on the other hand, you have an idea of how many bits you will need, you should create the BitSet with a specific initial size instead:

public BitSet(int numberOfBits)

Each bit in a BitSet is represented by a boolean value that can be examined, set, or cleared. Using an index value, you can set an individual bit to either true or false with the set or clear method, respectively:

public void set(int whichBit)
public void clear(int whichBit)

A BitSet automatically ...

Get Special Edition Using Java 2 Standard Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.