Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import java.util.BitSet;

public class Main {
    public static void main(String[] args) {
        BitSet bs = new BitSet();
        bs.set(31);
        bs.set(15);
        bs.set(18);
        int[] intArray = bits2Ints(bs);

        for (int i = 0; i < intArray.length; i++)
            System.out.println(toBinary(intArray[i]));
    }

    static int[] bits2Ints(BitSet bs) {
        int[] temp = new int[bs.size() / 32];

        for (int i = 0; i < temp.length; i++)
            for (int j = 0; j < 32; j++)
                if (bs.get(i * 32 + j))
                    temp[i] |= 1 << j;

        return temp;
    }

    static String toBinary(int num) {
        StringBuffer sb = new StringBuffer();

        for (int i = 0; i < 32; i++) {
            sb.append(((num & 1) == 1) ? '1' : '0');
            num >>= 1;
        }

        return sb.reverse().toString();
    }

}