get BitSet From Byte - Java java.util

Java examples for java.util:BitSet

Description

get BitSet From Byte

Demo Code


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

public class Main {
    public static void main(String[] argv) throws Exception {
        byte value = 2;
        int size = 2;
        System.out.println(getBitSetFromByte(value, size));
    }/*from   www.  j  ava  2s  .c  o  m*/

    public static BitSet getBitSetFromByte(byte value, int size) {
        BitSet bitset = new BitSet(size);
        for (int i = 0; i < size; i++) {
            if ((value & (1 << i)) > 0) {
                bitset.set(i);
            }
        }
        return bitset;
    }
}

Related Tutorials