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

voidbytesToBits(byte[] b, BitSet ba, int maxSize)
Read bits from a byte array into a bitset
int x = 0;
for (int i = 0; i < b.length; i++) {
    for (int j = 0; j < 8; j++) {
        if (x > maxSize)
            break;
        int mask = 1 << j;
        boolean value = (mask & b[i]) != 0;
        ba.set(x, value);
...
intcardinalityOf(BitSet bs)
cardinality = "total number of set bits"
return (bs == null ? 0 : bs.cardinality());
BitSetcheck(BitSet originalMessage, int messageLength, BitSet polynomial, int polynomialLength)
check
BitSet copy = originalMessage.get(0, messageLength);
for (int x = 0; x < messageLength; x++) {
    if (copy.get(0)) {
        copy.xor(polynomial);
    copy = copy.get(1, messageLength + polynomialLength - x);
return copy.get(0, polynomialLength);
...
booleancheckValidCharOnly(BitSet validChars, String value)
check Valid Char Only
for (char c : value.toCharArray()) {
    if (!validChars.get(c)) {
        return false;
return true;
voidclear(BitSet bitSet_)
Clear a BitSet .
bitSet_.and(EMPTY_BITSET);
BitSetcombine(int length, BitSet... bitsets)
combine a variable number of bitsets of the same length
BitSet combinedBitSet = new BitSet();
int currentBitset = 0;
for (BitSet bitset : bitsets) {
    for (int i = 0; i < length; i++) {
        if (bitset.get(i)) {
            combinedBitSet.set((currentBitset * length) + i);
    currentBitset++;
return combinedBitSet;
intcompare(BitSet a, BitSet b)
compare two bit sets
int i = a.nextSetBit(0);
int j = b.nextSetBit(0);
while (i == j) {
    if (i == -1)
        return 0;
    i = a.nextSetBit(i + 1);
    j = b.nextSetBit(j + 1);
if (i < j)
    return -1;
else
    return 1;
BitSetcopy(BitSet bs)
copy
return bs == null ? null : (BitSet) bs.clone();
BitSetcopyBits(BitSet source, BitSet dest, int fromIndex)
Copy bits completely from a source BitSet to a destination BitSet starting at the destination index
for (int i = 0; i < source.length(); i++) {
    if (source.get(i)) {
        dest.set(fromIndex + i);
return dest;
voidcopyBitSetToBitSet(BitSet src, int srcPos, BitSet dest, int destPos, int length)
copy Bit Set To Bit Set
for (int i = srcPos, j = destPos; i < length; ++i, ++j) {
    dest.set(j, src.get(i));