Java BitSet Create byte2BitSet(byte[] b, int offset, int maxBits)

Here you can find the source of byte2BitSet(byte[] b, int offset, int maxBits)

Description

Converts a binary representation of a Bitmap field into a Java BitSet

License

Open Source License

Parameter

Parameter Description
b - binary representation
offset - staring offset
maxBits - max number of bits (supports 64,128 or 192)

Return

java BitSet object

Declaration

public static BitSet byte2BitSet(byte[] b, int offset, int maxBits) 

Method Source Code


//package com.java2s;

import java.util.BitSet;

public class Main {
    /**//from w w w . j a  v a 2s. c  o m
     * Converts a binary representation of a Bitmap field into a Java BitSet
     * 
     * @param b - binary representation
     * @param offset - staring offset
     * @param maxBits - max number of bits (supports 64,128 or 192)
     * @return java BitSet object
     */
    public static BitSet byte2BitSet(byte[] b, int offset, int maxBits) {
        int len = maxBits > 64 ? ((b[offset] & 0x80) == 0x80 ? 128 : 64) : maxBits;
        if (maxBits > 128 && b.length > offset + 8 && (b[offset + 8] & 0x80) == 0x80) {
            len = 192;
        }

        BitSet bmap = new BitSet(len);
        for (int i = 0; i < len; i++) {
            if (((b[offset + (i >> 3)]) & (0x80 >> (i % 8))) > 0) {
                bmap.set(i + 1);
            }
        }
        return bmap;
    }
}

Related

  1. bitSetOf(long lowerBits, long upperBits)
  2. bitSetOf(String inString)
  3. bitSetOfIndexes(int... indexes)
  4. Byte2BitSet(byte bytes)
  5. byte2BitSet(byte[] b, int offset, boolean bitZeroMeansExtended)
  6. convert(final long value)
  7. convert(long value, int bitSetOffset, BitSet bits)
  8. convertIntToBitSet(int value)