Here you can find the source of decimalToBitString(int n, int min, int max)
public static String decimalToBitString(int n, int min, int max)
//package com.java2s; //License from project: Open Source License public class Main { public static String decimalToBitString(int n, int min, int max) { max -= min;//from w ww . j a va 2 s.co m n -= min; if (max == 0 || n < 0 || n > max) { return ""; } // find biggest power of 2 that fits between min and max int biggestPower = (int) logBaseN(max + 1, 2); int maxInBiggestPower = (int) Math.pow(2, biggestPower); if (n < maxInBiggestPower) { // n is translated to bits assuming the biggest power. The returned bit string will have "biggestPower" bits String bitString = Integer.toBinaryString(n); while (bitString.length() < biggestPower) { bitString = "0" + bitString; } return bitString; } else { // substract the biggest power range and recalculate return decimalToBitString(n - maxInBiggestPower, 0, max - maxInBiggestPower); } } public static double logBaseN(double n, double base) { return Math.log(n) / Math.log(base); } }