Skip to content

Instantly share code, notes, and snippets.

@yspreen
Last active October 30, 2018 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yspreen/6523b54c8f696265d0bec3f6487c6581 to your computer and use it in GitHub Desktop.
Save yspreen/6523b54c8f696265d0bec3f6487c6581 to your computer and use it in GitHub Desktop.
JS snippets
function toBin(i) {
let s = "", flip = i < 0;
i = flip ? ~i : i;
while (i != 0) {
s = (flip ? 1 - (i & 1) : (i & 1)) + s;
i >>= 1;
}
return (flip ? "…1" : "") + (s == "" ? (flip ? "1" : "0") : s);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment