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

BitSetstringToBitSet(String sbits)
Converts a string into a BitSet.
BitSet bits = new BitSet(sbits.length());
for (int i = sbits.length() - 1; i >= 0; i--) {
    bits.set(sbits.length() - 1 - i, (sbits.charAt(i) == '1'));
return bits;
char[]stripToCommonChars(char[] ac, BitSet commonChars)
strip To Common Chars
char[] ap = new char[ac.length];
int api = 0;
for (int i = 0; i < ac.length; i++) {
    if (commonChars.get(ac[i] - ASCII_LOWER)) {
        ap[api++] = ac[i];
return Arrays.copyOf(ap, api);
...
boolean[]toArray(BitSet bitset, int size)
Convert a BitSet into an array of boolean.
boolean[] b = new boolean[size];
for (int i = 0; i < size; i++)
    b[i] = bitset.get(i);
return b;
BitSettoBitSet(byte[] array)
to Bit Set
BitSet bitSet = new BitSet(8 * array.length);
for (int byteNo = 0; byteNo < array.length; byteNo++) {
    byte b = array[byteNo];
    for (int bitNo = 0; bitNo < 8; bitNo++) {
        if ((b & byteMask(bitNo)) != 0) {
            bitSet.set(8 * byteNo + bitNo);
return bitSet;
BitSettoBitSet(byte[] bytes)
Convert a byte array into a bit set.
BitSet bs = new BitSet();
for (int i = 0; i < bytes.length * 8; i++) {
    if ((bytes[i / 8] & (1 << (7 - (i % 8)))) > 0)
        bs.set(i, true);
    else
        bs.set(i, false);
return bs;
...
BitSettoBitSet(final byte[] bytes)
to Bit Set
BitSet bits = new BitSet();
if (bytes != null && bytes.length > 0) {
    for (int i = 0; i < (bytes.length * 8); i++) {
        if ((bytes[i / 8] & (1 << (7 - (i % 8)))) != 0) {
            bits.set(i);
return bits;
BitSettoBitSet(String data)
to Bit Set
BitSet ret = new BitSet(data.length());
for (int x = 0; x < data.length(); x++) {
    ret.set(x, data.charAt(x) == '1');
return ret;
BitSettoBitSet(String s, int length)
to Bit Set
int len = s.length();
BitSet bs = new BitSet(length);
for (int i = 0; i < len; i++) {
    if (s.charAt(i) == '1')
        bs.set(i);
return bs;
byte[]toByteArray(BitSet bits)
to Byte Array
byte[] bytes = new byte[bits.size() / 8];
for (int i = 0; i < bits.length(); i++) {
    if (bits.get(i)) {
        bytes[bytes.length - i / 8 - 1] |= 1 << (i % 8);
return bytes;
byte[]toByteArray(BitSet bits)
to Byte Array
byte[] bytes = new byte[bits.length() / 8 + 1];
for (int i = 0; i < bits.length(); i++) {
    if (bits.get(i)) {
        bytes[bytes.length - i / 8 - 1] |= 1 << (i % 8);
return bytes;