Java Utililty Methods BitSet to

List of utility methods to do BitSet to

Description

The list of methods to do BitSet to are organized into topic(s).

Method

intbitSetToUnsignedInt(BitSet b, int startBit, int length)
Convert a BitSet to an unsigned integer.
int val = 0;
int bitval = 1;
for (int i = 0; i < length; i++) {
    if (b.get(startBit + i))
        val += bitval;
    bitval += bitval;
return val;
...
byte[]bitsToBytes(BitSet ba, int size)
Pack the bits in ba into a byte[].
int bytesAlloc = countBytesForBits(size);
byte[] b = new byte[bytesAlloc];
for (int i = 0; i < b.length; i++) {
    short s = 0;
    for (int j = 0; j < 8; j++) {
        int idx = i * 8 + j;
        boolean val = (idx <= size && ba.get(idx));
        s |= (val ? (1 << j) : 0);
...
byte[]BitsToBytes(BitSet bits)
Bits To Bytes
return (BitsToBytes(bits, bits.length()));
StringbitsToHexString(BitSet ba, int size)
Pack the bits in ba into a byte[] then convert that to a hex string and return it.
return bytesToHex(bitsToBytes(ba, size));
longBitsToInt(BitSet bits, int length)
Bits To Int
int j = 0;
byte[] bytes = new byte[length / 8 + 1];
long result = 0;
for (int i = (length - 1); i >= 0; i--) {
    if (bits.get(i)) {
        bytes[bytes.length - j / 8 - 1] |= 1 << (j % 8);
    j++;
...
intconvert(BitSet bits)
convert
assert bits.length() <= 32;
int value = 0;
for (int i = 0; i < bits.length(); ++i) {
    value += bits.get(i) ? 1 << i : 0;
return value;
longconvert(BitSet bits)
convert
long value = 0L;
for (int i = 0; i < bits.length(); ++i) {
    value += bits.get(i) ? (1L << i) : 0L;
return value;