- // Pull leading zeros off
- var leadingZeros = [];
- while (base.ints[0] == "0") {
- leadingZeros.push("0");
- base.ints.shift();
- }
- // Convert leading zeros to binary equivalent
- var numBinLeadingZeros = Math.floor(Math.log2(base.asInt) * leadingZeros.length);
- var binLeadingZeros = "";
- for (var i=0; i<numBinLeadingZeros; i++) {
- binLeadingZeros += "0";
- }
- // Handle entropy of zero
- if (base.ints.length == 0) {
- return {
- binaryStr: binLeadingZeros,
- cleanStr: leadingZeros,
- base: base,
- }
- }
- // If the first integer is small, it must be padded with zeros.
- // Otherwise the chance of the first bit being 1 is 100%, which is
- // obviously incorrect.
- // This is not perfect for unusual bases, eg base 6 has 2.6 bits, so is
- // slightly biased toward having leading zeros, but it's still better
- // than ignoring it completely.
- // TODO: revise this, it seems very fishy. For example, in base 10, there are
- // 8 opportunities to start with 0 but only 2 to start with 1
- var firstInt = base.ints[0];
- var firstIntBits = Math.floor(Math.log2(firstInt))+1;
- var maxFirstIntBits = Math.floor(Math.log2(base.asInt-1))+1;
- var missingFirstIntBits = maxFirstIntBits - firstIntBits;
- var firstIntLeadingZeros = "";
- for (var i=0; i<missingFirstIntBits; i++) {
- binLeadingZeros += "0";
- }