Java BitSet bytesToBits(byte[] b, BitSet ba, int maxSize)

Here you can find the source of bytesToBits(byte[] b, BitSet ba, int maxSize)

Description

Read bits from a byte array into a bitset

License

Open Source License

Parameter

Parameter Description
b the byte[] to read from
ba the bitset to write to

Declaration

public static void bytesToBits(byte[] b, BitSet ba, int maxSize) 

Method Source Code


//package com.java2s;
/*//ww w .ja va2 s  .  c o m
 * JGrass - Free Open Source Java GIS http://www.jgrass.org 
 * (C) HydroloGIS - www.hydrologis.com 
 * 
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Library General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option) any
 * later version.
 * 
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU Library General Public License
 * along with this library; if not, write to the Free Foundation, Inc., 59
 * Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

import java.util.BitSet;

public class Main {
    /**
     * Read bits from a byte array into a bitset
     * @param b the byte[] to read from
     * @param ba the bitset to write to
     */
    public static void bytesToBits(byte[] b, BitSet ba, int maxSize) {
        int x = 0;
        for (int i = 0; i < b.length; i++) {
            for (int j = 0; j < 8; j++) {
                if (x > maxSize)
                    break;
                int mask = 1 << j;
                boolean value = (mask & b[i]) != 0;
                ba.set(x, value);
                x++;
            }
        }
    }
}

Related

  1. BitString2BitSet(String string)
  2. boolToBitSet(boolean[] bits, int offset, int length)
  3. byte2BitSet(BitSet bmap, byte[] b, int bitOffset)
  4. byteArray2BitSet(byte[] bytes)
  5. bytes2bitset(byte[] bytes, byte nbyte, BitSet bits)
  6. cardinalityOf(BitSet bs)
  7. check(BitSet originalMessage, int messageLength, BitSet polynomial, int polynomialLength)
  8. checkValidCharOnly(BitSet validChars, String value)
  9. clear(BitSet bitSet_)