Java Bits Convert to bitsToDouble(String bits, double min, double max, int splits)

Here you can find the source of bitsToDouble(String bits, double min, double max, int splits)

Description

Turns a bit string (0s and 1s) into a double.

License

Open Source License

Parameter

Parameter Description
bits the bit string
min the minimum
max the maximum
splits the number of splits

Return

the reconstructed double value

Declaration

public static double bitsToDouble(String bits, double min, double max, int splits) 

Method Source Code

//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)));
    }
}

Related

  1. bitsRequired(int value)
  2. bitsRequiredForFraction(String floatnumber)
  3. bitsString2(byte b)
  4. bitsTo8Bytes(boolean[] bits)
  5. bitsToBase64(byte data)
  6. bitsToFloats(boolean[] b)
  7. bitsToTransform(int src, int target)
  8. bitStr(long value)
  9. bitString(boolean value)