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

byte[]toByteArray(BitSet bits, int fixedNumBytes)
Converts a BitSet into an array of bytes
Double size = Math.ceil(new Double(fixedNumBytes) / 8);
byte[] bytes = new byte[size.intValue()];
for (int i = 0; i < fixedNumBytes; i++) {
    if (bits.get(i)) {
        bytes[bytes.length - i / 8 - 1] |= 1 << (i % 8);
return bytes;
...
byte[]toByteArray(BitSet bits, int sizeInBytes)
to Byte Array
byte[] bytes = new byte[sizeInBytes];
for (int i = 0; i < bits.length(); i++) {
    if (bits.get(i)) {
        bytes[i / 8] |= 1 << (i % 8);
return bytes;
byte[]toByteArray(BitSet bitSet)
to Byte Array
byte[] array = new byte[bitSet.size() / 8 + 1];
for (int i = bitSet.nextSetBit(0); i >= 0; i = bitSet.nextSetBit(i + 1)) {
    array[i / 8] |= byteMask(i);
return array;
byte[]toByteArray(BitSet bs, int length)
Convert a bit set into a byte array.
byte[] bytes = new byte[(int) Math.ceil((double) length / 8)];
for (int i = 0; i < length; i++) {
    if (bs.get(i))
        bytes[i / 8] |= 1 << (7 - (i % 8));
return bytes;
byte[]toFixedLengthByteArray(BitSet bs, int length)
Converts a BitSet to a byte[] including length bytes (length*8 bits).
BitSet prefix = bs.get(0, length * 8);
BitSet lastByte = prefix.get((length - 1) * 8, length * 8);
if (lastByte.cardinality() > 0) { 
    return prefix.toByteArray();
} else { 
    prefix.set(length * 8 - 1);
    byte[] a = prefix.toByteArray();
    a[a.length - 1] = 0;
...
BitSettoggleInPlace(BitSet a, BitSet b)
a perhaps curious method: b is a reference set, perhaps all atoms in a certain molecule a is the working set, perhaps representing all displayed atoms For each set bit in b: a) if a is also set, then clear a's bit UNLESS b) if a is not set, then add to a all set bits of b Thus, if a equals b --> clear all if a is a subset of b, then --> b if b is a subset of a, then --> a not b if a only intersects with b, then --> a or b if a does not intersect with b, then a or b In "toggle" mode, when you click on any atom of the molecule, you want either: (a) all the atoms in the molecule to be displayed if not all are already displayed, or (b) the whole molecule to be hidden if all the atoms of the molecule are already displayed.
if (a.equals(b)) {
    a.clear();
} else if (andNot(copy(b), a).length() == 0) {
    andNot(a, b);
} else {
    a.or(b);
return a;
...
inttoInt(BitSet bitSet)
to Int
int intValue = 0;
for (int bit = 0; bit < bitSet.length(); bit++) {
    if (bitSet.get(bit)) {
        intValue |= (1 << bit);
return intValue;
int[]toIntArray(final BitSet bits, final int size)
to Int Array
final int[] array = new int[size];
for (int i = 0; i < array.length; i++) {
    for (int j = 0; j < 32; j++) {
        if (bits.get(i * 32 + j)) {
            array[i] |= 1 << j;
return array;
inttoInteger(final BitSet bits)
to Integer
int value = 0;
for (int i = 0; i < 32; ++i) {
    value += bits.get(i) ? (1 << i) : 0;
return value;
longtoLong(final BitSet value)
Converts BitSet to long value if possible.
long longValue = 0;
for (int i = Math.min(value.length() - 1, 63); i >= 0; i--) {
    longValue <<= 1;
    if (value.get(i)) {
        longValue++;
return longValue;
...