java.util.BitSet class

suggest change

java.util.BitSet has been in the platform since Java 1.0. It stores and manipulates a growable set of bits. The IntStream in the example is Java 8:

final BitSet bitSet = new BitSet(8); // by default all bits are unset

IntStream.range(0, 8).filter(i -> i % 2 == 0).forEach(bitSet::set); // {0, 2, 4, 6}

bitSet.set(3); // {0, 2, 3, 4, 6}

bitSet.set(3, false); // {0, 2, 4, 6}

final boolean b = bitSet.get(3); // b = false

bitSet.flip(6); // {0, 2, 4}

bitSet.set(100); // {0, 2, 4, 100} - expands automatically

BitSet implements Cloneable and Serializable. The OpenJDK implementation stores the bits in a private long array and grows that array when a higher index is set. Other JDKs need not use a field named words.

It also supports whole-set logical operations and, or, xor, andNot:

bitSet.and(new BitSet(8));
bitSet.or(new BitSet(8));
bitSet.xor(new BitSet(8));
bitSet.andNot(new BitSet(8));

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents