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

voidensureExclusivity(BitSet bs1, BitSet bs2)
Adjust two bitsets so that they do not overlap
if (bs1.cardinality() > bs2.cardinality()) {
    bs1.andNot(bs2);
    bs2.andNot(bs1);
} else {
    bs2.andNot(bs1);
    bs1.andNot(bs2);
Stringescape(String s, BitSet safeChars)
Escapes the given string using URL encoding for all bytes not included in the given set of safe characters.
byte[] bytes = s.getBytes();
StringBuilder out = new StringBuilder(bytes.length);
for (int i = 0; i < bytes.length; i++) {
    int c = bytes[i] & 0xff;
    if (safeChars.get(c) && c != ESCAPE_CHAR) {
        out.append((char) c);
    } else {
        out.append(ESCAPE_CHAR);
...
BitSetexpand(final BitSet bits, final BitSet add)
Fuses two given BitSets to one by adding the bits of the second at the end of the first BitSet.
final BitSet result = (BitSet) bits.clone();
int index = result.length();
for (int i = 0; i < add.length(); i++) {
    if (add.get(i)) {
        result.set(index);
    index++;
return result;
longextractLong(BitSet bitSet, int from, int to)
extract Long
long[] longArray = bitSet.get(from, to).toLongArray();
long result = 0;
if (longArray.length > 0) {
    result = longArray[0];
return result;
StringextractSetBitsMsgFromTxSet(final BitSet txSet)
extract Set Bits Msg From Tx Set
if (txSet.isEmpty()) {
    return null;
final StringBuffer result = new StringBuffer();
final int lastIndex = txSet.size() - 1;
int setIndex = txSet.nextSetBit(0);
do {
    result.append(setIndex);
...
double[]filter(double[] data, BitSet mask)
filter
double[] result = new double[mask.cardinality()];
int j = 0;
for (int i = 0; i < data.length; i++) {
    if (mask.get(i)) {
        result[j] = data[i];
        j++;
return result;
ListfilterByIndices(List list, BitSet filter)
filter By Indices
List<E> result = new ArrayList<>(list.size());
for (int i = 0; i < list.size(); i++) {
    E element = list.get(i);
    if (filter.get(i)) {
        result.add(element);
return result;
...
Iteratorfiltered(final Iterator iterator, final BitSet filter)
Returns an Iterator which returns a filtered subset of the given Iterator based on the given filter BitSet
if (iterator == null) {
    return empty();
return new Iterator<E>() {
    private int index = 0;
    private boolean hasNoMoreFilteredElements = false;
    @Override
    public boolean hasNext() {
...
int[]findTrue(BitSet set)
Returns an array containing all numbers of bits that are set to true.
int[] ret = new int[set.cardinality()];
int offset = 0;
for (int idx = set.nextSetBit(0); idx >= 0; idx = set.nextSetBit(idx + 1)) {
    ret[offset++] = idx;
return ret;
intfirstIndexOfChar(String sqlString, BitSet keys, int startindex)
first Index Of Char
for (int i = startindex, size = sqlString.length(); i < size; i++) {
    if (keys.get(sqlString.charAt(i))) {
        return i;
return -1;