module BinaryDigits { @Inject Console console; void run() { Int64[] tests = [0, 1, 5, 50, 9000]; Int longestInt = tests.map(n -> n.estimateStringLength()) .reduce(0, (max, len) -> max.notLessThan(len)); Int longestBin = tests.map(n -> (64-n.leadingZeroCount).notLessThan(1)) .reduce(0, (max, len) -> max.maxOf(len)); function String(Int64) num = n -> { Int indent = longestInt - n.estimateStringLength(); return $"{' ' * indent}{n}"; }; function String(Int64) bin = n -> { Int index = n.leadingZeroCount.minOf(63); Int indent = index - (64 - longestBin); val bits = n.toBitArray()[index ..< 64]; return $"{' ' * indent}{bits.toString().substring(2)}"; }; for (Int64 test : tests) { console.print($"The decimal value {num(test)} should produce an output of {bin(test)}"); } } }