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

intcountLeadingSimilarBits(BitSet a, BitSet b)
count Leading Similar Bits
int result = 0;
for (int i = 127; i >= 0 && (a.get(i) == b.get(i)); i--) {
    result++;
return result;
BitSetcreateBitSetFromBinaryString(String bitsAsBinaryString)
converts a string to a bitset counting all 1's as true and all other characters as false
BitSet bitSet = new BitSet(bitsAsBinaryString.length());
for (int i = 0; i < bitsAsBinaryString.length(); i++) {
    if (bitsAsBinaryString.substring(i, i + 1).equals("1")) {
        bitSet.set(i, true);
    } else {
        bitSet.set(i, false);
return bitSet;
BitSetcreateSimpleBitSet(Set setBits)
create Simple Bit Set
BitSet retval = new BitSet();
for (int i : setBits) {
    retval.set(i);
return retval;
BitSetdecode(BitSet encoded, int encodedBitLength)
decode
int decodedBitLength = encodedBitLength - (int) (Math.log(encodedBitLength) / Math.log(2)) - 1;
BitSet decoded = new BitSet(decodedBitLength);
for (int i = encodedBitLength - 1, j = decodedBitLength - 1; i >= 0; --i) {
    if (Math.log(encodedBitLength - i) / Math.log(2) % 1 != 0) {
        decoded.set(j, encoded.get(i));
        --j;
return decoded;
longdecodeIndex(final BitSet bs, final long rangePerDimension)
decode Index
long floor = 0;
long ceiling = rangePerDimension;
long mid = 0;
for (int i = 0; i < bs.length(); i++) {
    mid = (floor + ceiling) / 2;
    if (bs.get(i)) {
        floor = mid;
    } else {
...
BitSetdeleteBits(BitSet bs, BitSet bsDelete)
this one slides deleted bits out of a pattern.
if (bs == null || bsDelete == null)
    return bs;
int ipt = bsDelete.nextSetBit(0);
if (ipt < 0)
    return bs;
int len = bs.length();
int lend = Math.min(len, bsDelete.length());
int i;
...
voiddualNext(BitSet b)
dual Next
int index = b.nextClearBit(0);
b.set(index);
b.flip(0, index);
Stringencode(BitSet allowedCharacters, String s, String charset)
encode
if (s == null)
    return null;
final StringBuilder encoded = new StringBuilder(s.length() * 3);
final char[] characters = s.toCharArray();
try {
    for (char c : characters) {
        if (allowedCharacters.get(c)) {
            encoded.append(c);
...
BitSetencode(BitSet origin, int originBitLength)
encode
int encodedBitLength = (int) (originBitLength + Math.log(originBitLength) / Math.log(2) + 1);
BitSet encoded = new BitSet(encodedBitLength);
for (int i = 1, j = originBitLength - 1; i <= encodedBitLength; ++i) {
    if (Math.log(i) / Math.log(2) % 1 == 0) {
        encoded.set(encodedBitLength - i, false);
    } else {
        encoded.set(encodedBitLength - i, origin.get(j));
        --j;
...
Stringencode(String s, BitSet bs)
encode
StringBuilder buf = null;
int len = s.length();
for (int i = 0; i < len; i++) {
    int ch = s.charAt(i);
    if (bs.get(ch)) {
        if (buf != null)
            buf.append((char) ch);
    } else {
...