byte array to BitSet - Java java.util

Java examples for java.util:BitSet

Description

byte array to BitSet

Demo Code


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

public class Main {
    public static void main(String[] argv) throws Exception {
        byte flags = 2;
        System.out.println(toBitSet(flags));
    }//from w ww  .ja  v  a2  s  .c om

    public static BitSet toBitSet(byte flags) {
        // convert the byte into a @{link BitSet}
        BitSet bs = new BitSet(8);
        for (int i = 0; i < 8; i++) {
            boolean flag = (flags & (1 << i)) != 0;
            bs.set(i, flag);
        }
        return bs;
    }
}

Related Tutorials