Java Utililty Methods BitSet

List of utility methods to do BitSet

Description

The list of methods to do BitSet are organized into topic(s).

Method

booleanisSubset(BitSet bits1, BitSet bits2)
True if all bits set in first bitset are also set in second.
int index = -1;
while (true) {
    if (0 > (index = bits1.nextSetBit(++index)))
        break;
    if (!bits2.get(index))
        return false;
return true;
...
booleanisSubset(BitSet subsetToTest, BitSet supersetToTest)
Determine if the 1st argument is a subset of the 2nd.
BitSet intersection = (BitSet) supersetToTest.clone();
intersection.and(subsetToTest);
return intersection.equals(subsetToTest);
booleanisSubSet(BitSet x, BitSet y)
very inefficient, but Java wonderful bitset has no subset op perhaps using bit iterator would be faster, I can't be bothered.
y = (BitSet) y.clone();
y.and(x);
return y.equals(x);
longlongFrom(final BitSet bitSet)
Creates an long out of a bitset
return longFrom(bitSet, NBITS_LONG_REPRESENTATION);
BitSetlongToBitSet(long bits)
long To Bit Set
BitSet ret = new BitSet();
for (int count = 0;; count++) {
    long curBit = 1L << count;
    ret.set(count, (bits & curBit) != 0);
    if (curBit >= bits)
        break;
return ret;
...
BitSetlongToBitSet(long value)
Convert a BitSet to its equivalent integer (long) value.
BitSet bits = new BitSet(Long.SIZE);
int index = 0;
while (value != 0L) {
    long rem = value % 2L;
    if (rem != 0) {
        bits.set(index);
    ++index;
...
BitSetmapToBitSet(Map map, String key)
Parses a string representation of a BitSet into a BitSet7 object.
String sbits = map.get(key);
if (sbits == null)
    return null;
else
    return stringToBitSet(sbits);
intmax(BitSet bits)
get the largest member of the set
int max = 0;
for (int value = bits.nextSetBit(0); value != -1; value = bits.nextSetBit(value + 1)) {
    max = value;
return max;
Iterablemembers(BitSet set)
iterable over all members
return () -> new Iterator<Integer>() {
    private int i = set.nextSetBit(0);
    @Override
    public boolean hasNext() {
        return i != -1;
    @Override
    public Integer next() {
...
intnextClearBitModulo(int index, int poolSize, BitSet bitSet)
Returns index of next clear (false) bit, starting from and including index.
int indexToSet = bitSet.nextClearBit(index);
if (indexToSet == poolSize && index != 0) {
    indexToSet = bitSet.nextClearBit(0);
if (indexToSet == poolSize) {
    return -1;
return indexToSet;
...