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

int[]asArrayWith0s(int max, BitSet bits)
gets an array in which the i-th component has value i, if and only if i is contained in bits
final int[] array = new int[max + 1];
for (Integer t : members(bits))
    array[t] = t;
return array;
BitSetbinaryStringToBitSet(String stringRepresentation)
Convert the string representation to a bit set
BitSet bitSet = new BitSet(stringRepresentation.length());
for (int i = 0; i < stringRepresentation.length(); i++) {
    if (stringRepresentation.charAt(i) == '1') {
        bitSet.set(i);
    } else if (stringRepresentation.charAt(i) != '0') {
        throw new IllegalArgumentException("bad format: " + stringRepresentation);
return bitSet;
BitSetbinaryToGray(BitSet binary)
binary To Gray
if (binary.equals(ZERO)) {
    return (BitSet) ZERO.clone();
} else {
    BitSet gray = (BitSet) binary.clone();
    gray.xor(binary.get(1, binary.length()));
    return gray;
voidbitsetCopy(BitSet src, int srcOffset, BitSet dest, int destOffset, int length)
bitset Copy
for (int i = 0; i < length; i++) {
    dest.set(destOffset + i, src.get(srcOffset + i));
booleanbitSetTest(BitSet bitSet, int index)
Check to see if the i-th bit is set.
if (bitSet == null)
    return false;
if (index < 0)
    return false;
return bitSet.get(index);
BitSetBitString2BitSet(String string)
Converts the given {0,1}-String to a BitSet .
char[] chStr = string.toCharArray();
BitSet bitset = new BitSet(chStr.length);
for (int i = 0; i < chStr.length; i++) {
    if (chStr[chStr.length - i - 1] == '1') {
        bitset.set(i);
return bitset;
...
BitSetboolToBitSet(boolean[] bits, int offset, int length)
Converts a boolean array into a BitSet
BitSet bitset = new BitSet(length - offset);
for (int i = offset; i < length; i++)
    bitset.set(i - offset, bits[i]);
return bitset;
BitSetbyte2BitSet(BitSet bmap, byte[] b, int bitOffset)
Converts a binary representation of a Bitmap field into a Java BitSet
int len = b.length << 3;
for (int i = 0; i < len; i++)
    if (((b[i >> 3]) & (0x80 >> (i % 8))) > 0)
        bmap.set(bitOffset + i + 1);
return bmap;
BitSetbyteArray2BitSet(byte[] bytes)
Returns a bitset containing the values in bytes.
BitSet bits = new BitSet();
for (int i = 0; i < bytes.length * 8; i++) {
    if ((bytes[bytes.length - i / 8 - 1] & (1 << (i % 8))) > 0) {
        bits.set(i);
return bits;
BitSetbytes2bitset(byte[] bytes, byte nbyte, BitSet bits)
bytesbitset
if (bytes == null) {
    return bits;
for (int i = 0; i < nbyte * 8; i++) {
    if ((bytes[nbyte - i / 8 - 1] & (1 << (i % 8))) > 0) {
        bits.set(i);
return bits;