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

intfirstInvalidOctet(CharSequence cs, BitSet bits)
first Invalid Octet
for (int i = 0; i < cs.length(); i++) {
    char c = cs.charAt(i);
    if (!bits.get(c)) {
        return i;
return -1;
StringformatBitset(BitSet bitset, int width)
format Bitset
StringBuilder str = new StringBuilder();
for (int i = (width - 1); i >= 0; i--) {
    if (bitset.get(i)) {
        str.append("1");
    } else {
        str.append("0");
return str.toString();
BitSetgenerateBitSetBySize(int size, boolean initValue)
generate a BitSet by size
BitSet bitSet = new BitSet(size);
if (initValue) {
    bitSet.set(0, size);
return bitSet;
intgetBiggerRang(BitSet bitSet)
Retrun bigger rang from bit set.
for (int i = bitSet.size() - 1; i >= 0; i--) {
    if (bitSet.get(i))
        return i;
return 0;
BitSetgetBitSet()
get Bit Set
return new BitSet();
BitSetgetBitSetFromBooleanArray(boolean[] buf)
Convert a boolean[] into an instance of our value class.
BitSet set = new BitSet();
for (int i = 0; i < buf.length; i++) {
    if (buf[i]) {
        set.set(i);
return set;
StringgetLemma(String[] tokens, BitSet bits, String delimiter)
get Lemma
StringBuffer buf = new StringBuffer();
for (int i = 0; i < tokens.length; i++) {
    if (i != 0 && !bits.get(i - 1)) {
        buf.append(delimiter);
    buf.append(tokens[i]);
return buf.toString();
...
int[]getSetBitIndices(BitSet bitSet)
Convert a bit representation of a set to its index representation
int[] setBitIndices = new int[bitSet.cardinality()];
int currSetBitIndex = 0;
int currBitIndex = 0;
while (currSetBitIndex < setBitIndices.length) {
    if (bitSet.get(currBitIndex)) {
        setBitIndices[currSetBitIndex] = currBitIndex;
        currSetBitIndex++;
    currBitIndex++;
return setBitIndices;
inthammingDistance(BitSet bs1, BitSet bs2)
hamming Distance
BitSet ref = (BitSet) bs1.clone(); 
ref.and(bs2);
return (bs1.cardinality() - ref.cardinality());
booleanhasAtLeastOneBitSet(BitSet bitSet, Iterable indexes)
Returns true if at least one bit on a given position is set.
for (Integer index : indexes) {
    if (bitSet.get(index)) {
        return true;
return false;