Skip to content

Instantly share code, notes, and snippets.

@nizarmah
Last active November 11, 2017 12:30
Show Gist options
  • Save nizarmah/94eee07bee1aba368650599f7361f229 to your computer and use it in GitHub Desktop.
Save nizarmah/94eee07bee1aba368650599f7361f229 to your computer and use it in GitHub Desktop.
IEEE 754 Converter : Any Bit Float to Binary and Back
/*
Floating Bin JS Script
Nizar Mahmoud https://nizarmah.me/
IEEE 754 Converter
Float to Binary
Binary to Float
Bits : 16, 32, 64, 128, 256
Use freely, just would appreciate
keeping this comment on top to
show me off to other people
Thanks to https://twitter.com/MrNoahz
for being a thorough tester of this
And thanks to stackoverflow and the
kind dudes phimuemue and Rudy
https://stackoverflow.com/questions/6910115/
how-to-represent-float-number-in-memory-in-c#6910674
*/
function _convert(string, base) {
string = string.toString().trim().split(".");
base = +base || 2;
return (parseInt(string[0].replace("-", ""), base) + (string[1] || "").split("").reduceRight((sum, bit) => (sum + parseInt(bit, base)) / base, 0)) * (+(string[0][0] !== "-") || -1);
}
function _sniffer(bit) {
var expSize;
switch (bit) {
case 16:
expSize = 5;
break;
case 32:
expSize = 8;
break;
case 64:
expSize = 11;
break;
case 128:
expSize = 15;
break;
case 256:
expSize = 19;
break;
default: return false;
}
return { exp: expSize, flt: bit - (expSize + 1), bias: ((Math.pow(2, (expSize - 1))) - 1) };
}
function flt2bin(flt, bit) {
var bin = "";
var binList;
bit = bit || 32;
binSizes = _sniffer(bit);
if (!binSizes)
return false;
if (flt == Infinity || flt == -Infinity || flt.toString() == "NaN") {
if (flt == -Infinity)
bin += "1";
else bin += "0";
var pow = "";
while (pow.length < binSizes.exp)
pow += "1";
bin += pow;
if (flt == NaN)
bin += "1";
while (bin.length < bit)
bin += "0";
return bin;
}
if (flt >= 0)
bin += "0";
else bin += "1";
flt = Math.abs(flt);
if (!flt.toString().indexOf(".")) {
binList = [ (flt).toString(2) ];
} else binList = (flt).toString(2).split(".");
var exp;
if (binList[0] != 0) {
exp = binList[0].length - 1;
} else {
if (binList.length > 1) {
if (binList[1].indexOf("1") > -1) {
exp = -1 * (binList[1].indexOf("1") + 1);
} else exp = -binSizes.bias;
} else exp = -binSizes.bias;
}
var expBias = (exp + binSizes.bias).toString(2).split("");
while (expBias.length < binSizes.exp)
expBias.unshift("0");
bin += expBias.toString().replace(/,/g, '');
if (exp >= 0) {
bin += binList[0].substr(1, binList[0].length);
if (binList.length > 1)
bin += binList[1];
} else {
if (binList.length > 1)
bin += binList[1].substr(Math.abs(exp), binList[1].length);
else bin += "0";
}
if (bin.length >= bit) {
bin = bin.substr(0, bit);
} else {
while (bin.length < bit)
bin += "0";
}
return bin;
}
function bin2flt(bin, bit) {
var binList = bin.split("");
bit = bit || 32;
binSizes = _sniffer(bit);
if (!binSizes)
return false;
var s, e, m;
for (var i = 0; i < bit; i++) {
if (i < 1) {
s = binList[i];
} else if (i < (binSizes.exp + 1)) {
if (i == 1) e = binList[i];
else e += binList[i];
} else if (i < binList.length) {
if (i == (binList.length - binSizes.flt)) m = binList[i];
else m += binList[i];
}
}
if (e.indexOf("0") < 0) {
if (m.indexOf("1") < 0) {
if (s == 1)
return -Infinity;
else return Infinity;
} else return NaN;
}
if (e.indexOf("1") < 0)
return 0;
if (s == "1") s = "-1";
else s = "1";
e = _convert(e, 2);
e = e - binSizes.bias;
m = "1." + m;
m = _convert(m, 2);
return (s * m * Math.pow(2, e));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment