Answer with a BitSet initialized from a byte array representation of a bitmap - Java java.util

Java examples for java.util:BitSet

Description

Answer with a BitSet initialized from a byte array representation of a bitmap

Demo Code


//package com.java2s;
import java.util.BitSet;

public class Main {
    public static void main(String[] argv) throws Exception {
        byte[] binBitmap = new byte[] { 34, 35, 36, 37, 37, 37, 67, 68, 69 };
        System.out.println(bin2Bitset(binBitmap));
    }/*  w  w  w .java  2 s.c o m*/

    /**
     * Answer with a BitSet initialized from a byte array representation of a bitmap
     * @param binBitmap
     * @return
     */
    static BitSet bin2Bitset(final byte[] binBitmap) {
        final BitSet result = new BitSet();
        for (int bytenum = 0; bytenum < binBitmap.length; bytenum++) {
            for (int bit = 0, mask = 0x80; mask >= 0x01; bit++, mask /= 2) {
                if ((mask & binBitmap[bytenum]) == mask) {
                    result.set((bytenum * 8) + bit);
                }
            }
        }

        return result;
    }
}

Related Tutorials