Here you can find the source of toBinary(int nr, int bits)
public static String toBinary(int nr, int bits)
//package com.java2s; //License from project: Open Source License public class Main { public static String toBinary(int nr, int bits) { String binval = ""; int val; int i = bits; do {/*from ww w . j a va 2 s .c om*/ val = power(2, i); if (nr > val) { binval = binval + "1"; nr = nr - val; } else { binval = binval + "0"; } i--; } while (i > 0); return binval; } public static int power(int nr, int pwr) { int retval = 1; for (int i = 0; i < pwr; i++) { retval = retval * nr; } return retval; } }