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

BitSetnot(BitSet bitSets)
not
bitSets.flip(0, bitSets.size());
return bitSets;
BitSetor(BitSet bs1, BitSet bs2)
Logical OR two bit sets without modifying the source.
BitSet bsr = (BitSet) bs1.clone();
bsr.or(bs2);
return bsr;
Objectpack(BitSet bitSet)
pack
List<Long> values = new ArrayList<Long>();
long value = 0L;
int j = 0;
for (int i = 0; i < bitSet.size();) {
    if (bitSet.get(i)) {
        value |= (1L << j);
    i += 1;
...
BitSetpadToByteBoundary(BitSet actualParameter, int sizeInBits, int byteBoundLength, boolean preserveFirstAsSign)
pad To Byte Boundary
BitSet paddedSet = new BitSet(byteBoundLength);
int numPaddingBits = byteBoundLength - sizeInBits;
if (preserveFirstAsSign && actualParameter.get(0)) {
    for (int i = 0; i < numPaddingBits; i++) {
        paddedSet.set(i);
for (int i = numPaddingBits; i < byteBoundLength; i++) {
...
BitSetparseBitSet(byte[] sfData, int offset, int length)
parse Bit Set
BitSet bitSet = new BitSet(length * 8);
int pos = 0;
for (int i = 0; i < length; i++) {
    byte b = sfData[offset + i];
    for (int j = 0; j < 8; j++) {
        boolean val = (b & (0x01 << 7 - j)) != 0;
        bitSet.set(pos, val);
        pos++;
...
intpickRandomSetIndexFromBitSet(BitSet bitset)
pick Random Set Index From Bit Set
if (bitset.isEmpty()) {
    throw new RuntimeException("The bitset is empty, cannot find a set element");
String set = bitset.toString();
String[] indexes = set.substring(1, set.length() - 1).split(",");
return Integer.parseInt(indexes[(int) (Math.random() * (indexes.length - 1))].trim());
StringprintBitSet(BitSet bs)
print Bit Set
return bs.toString();
voidprintBitSet(BitSet iBits, int length)
print Bit Set
System.out.println("--------------------------------");
System.out.println("BitSet length is " + iBits.length());
System.out.println("length is " + length);
for (int i = 0; i < length; i++) {
    if (((i - 1) % 8) == 0) {
        System.out.print(" ");
    if (iBits.get(i)) {
...
bytereadByte(BitSet bits, int startByte)
read Byte
byte res = 0;
for (int k = 0; k < 8; k++) {
    if (bits.get(startByte * 8 + k)) {
        res = (byte) (res | (1 << (7 - k)));
return res;
BitSetreverse(final BitSet bitset, final int sizeInBits)
reverse
final BitSet reversed = new BitSet();
int reversedIndex = 0;
for (int i = sizeInBits - 1; i >= 0; i--) {
    if (bitset.get(i)) {
        reversed.set(reversedIndex);
    reversedIndex++;
return reversed;