Here you can find the source of bitsToDouble(String bits, double min, double max, int splits)
Parameter | Description |
---|---|
bits | the bit string |
min | the minimum |
max | the maximum |
splits | the number of splits |
public static double bitsToDouble(String bits, double min, double max, int splits)
//package com.java2s; /*/*from w w w . j a v a2 s .c o m*/ * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ public class Main { /** * Turns a bit string (0s and 1s) into a double. * * @param bits the bit string * @param min the minimum * @param max the maximum * @param splits the number of splits * @return the reconstructed double value */ public static double bitsToDouble(String bits, double min, double max, int splits) { double j = 0; for (int i = 0; i < bits.length(); i++) { if (bits.charAt(i) == '1') { j = j + Math.pow(2, bits.length() - i - 1); } } j = Math.min(j, splits); return (min + j * ((max - min) / (double) (splits - 1))); } }