Created
September 11, 2021 07:10
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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