Created
August 29, 2018 21:16
-
-
Save brawaru/49fdb0ff39e956867c205327a6ad03ad to your computer and use it in GitHub Desktop.
Example of Binary-to-Decimal conversion function for JavaScript with BigInt support
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 bin2dec(b) { | |
| if (!/^[0-1]+$/.test(b)) { | |
| throw new Error("Invalid binary value"); | |
| } | |
| let d = 0n; | |
| for (let i = 0, l = b.length; i < l; i++) { | |
| d = d * 2n; | |
| if (b[i] === "1") { | |
| d += 1n; | |
| } | |
| } | |
| return d + ""; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment