Skip to content

Instantly share code, notes, and snippets.

@edvakf
Created June 18, 2010 09:03
Show Gist options
  • Save edvakf/443421 to your computer and use it in GitHub Desktop.
Save edvakf/443421 to your computer and use it in GitHub Desktop.
hash = _bit2num;
sign = mix < 0;
sign && (mix *= -1);
exp = Math.log(mix) / Math.LN2 + 1023 | 0; // add offset 1023 to ensure positive
frac = (Math.floor(mix * Math.pow(2, 52 + 1023 - exp))).toString(2).slice(1); // shift 52 - (exp - 1023) bits to make integer part exactly 53 bits, then throw away trash less than decimal point
exp = ("000000000" + exp.toString(2)).slice(-11); // exp is between 1 and 2047. make it 11 bits
ary = (+sign + exp + frac).match(_split8char);
rv.push(0xcb, hash[ary[0]], hash[ary[1]],
hash[ary[2]], hash[ary[3]],
hash[ary[4]], hash[ary[5]],
hash[ary[6]], hash[ary[7]]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment