Skip to content

Instantly share code, notes, and snippets.

@raypulver
Created December 1, 2017 21:39
Show Gist options
  • Save raypulver/9bbcc4fd768d95aa070b4c406f220c13 to your computer and use it in GitHub Desktop.
Save raypulver/9bbcc4fd768d95aa070b4c406f220c13 to your computer and use it in GitHub Desktop.
show me that double
'use strict';
const number = 100;
// see the actual bits, since JS uses 64-bit doubles
let buffer = new ArrayBuffer(8);
let doubleLoader = new Float64Array(buffer);
doubleLoader[0] = number;
let bytes = new Uint8Array(buffer);
// output the bytes
console.log([].slice.call(bytes));
// give me pad left or give me death
const padLeft = (s, char, len) => s.length >= len ? s : Array(len - s.length + 1).join(char) + s;
// output the bits
console.log([].map.call(bytes, (v) => padLeft(v.toString(2), '0', 8)).join(''));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment