Skip to content

Instantly share code, notes, and snippets.

@000407
Created September 11, 2021 07:10
Show Gist options
  • Select an option

  • Save 000407/55fd103e1556ac323744333396d67a81 to your computer and use it in GitHub Desktop.

Select an option

Save 000407/55fd103e1556ac323744333396d67a81 to your computer and use it in GitHub Desktop.
JS function that converts a given number to a 2's complement of a given length
function BINARIZE(dec, len)
{
if (isNaN(len) || len <= 0) {
throw new Error("Invalid length. Should be a number > 0");
}
if (dec < 0) {
dec = Math.pow(2, len) - -dec;
}
return dec.toString(2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment