get BitSet From Int - Java java.util

Java examples for java.util:BitSet

Description

get BitSet From Int

Demo Code


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

public class Main {
    public static void main(String[] argv) throws Exception {
        int value = 2;
        int size = 2;
        System.out.println(getBitSetFromInt(value, size));
    }/* ww  w.j  av a 2 s  .  c o m*/

    public static BitSet getBitSetFromInt(int value, int size) {
        BitSet bits = new BitSet(size);
        boolean bitValue;
        int index = 0;
        while (value > 0) {
            if ((value % 2) == 0)
                bitValue = false;
            else
                bitValue = true;
            bits.set(index++, bitValue);
            value /= 2;
        }
        return bits;
    }
}

Related Tutorials