Invert GMLscripts.com

fraction_to_bin

Converts a fractional value to a string of binary digits.

bin = fraction_to_bin(0.12500, 5);  // bin == "00100"
bin = fraction_to_bin(0.65625, 5);  // bin == "10101"
bin = fraction_to_bin(0.96875, 5);  // bin == "11111"
fraction_to_bin(value,size)
Returns a string of binary digits (1 bit each) representing the given decimal fraction.
COPY/// fraction_to_bin(value,size)
//
//  Returns a string of binary digits (1 bit each)
//  representing the given decimal fraction.
//
//      value       fraction, real
//      size        number of bits, real
//
/// GMLscripts.com/license
{
    var i, ret;
    i = 0;
    ret = "";
    repeat (argument1) {
        if (argument0 >= 1/(2<<i)) {
            ret += "1";
            argument0 -= 1/(2<<i);
        } else {
            ret += "0";
        }
        i += 1;
    }
    return ret;
}

Contributors: Bryan

GitHub: View · Commits · Blame · Raw