Hong-Phuc Bui
2025-01-16 2889de7f0c2d587a17fbd322af57c29e84238620
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/*
 * binary.js
 *
 */
 
import {terminal} from "./dfhi.js";
 
window.main = function(...argv) {
    let n = BigInt(argv[0]);
    let power = 1n;
    const LIMIT = n / 2n;
    // find the largest power of 2, that <= n/2
    while(power <= LIMIT) {
        power *= 2n;
    }
    let convert = "";
    while(power > 0) {
        if (n < power) {
            convert += "0";
        } else {
            convert += "1";
            n -= power;
        }
        power /= 2n;
    }
    terminal.printl(convert);
};