Binary digits: Difference between revisions
m
→{{header|Ecstasy}}
(Added S-BASIC example) |
|||
Line 2,140:
=={{header|Ecstasy}}==
<syntaxhighlight lang="java">
module BinaryDigits {
@Inject Console console;
void run() {
▲ Int[] tests = [0, 1, 5, 50, 9000];
Int longestInt = tests.map(n -> n.estimateStringLength())
Int longestBin = tests.map(n -> (64-n.leadingZeroCount).notLessThan(1))
.reduce(0, (max, len) -> max.maxOf(len));
function String(
Int indent = longestInt - n.estimateStringLength();
return $"{' ' * indent}{n}";
function String(
Int index = n.leadingZeroCount.minOf(63);
Int indent = index - (64 - longestBin);
val bits = n.toBitArray()[index ..< 64];
return $"{' ' * indent}{bits.toString().substring(2)}";
for (
console.print($"The decimal value {num(test)} should produce an output of {bin(test)}");
}
}
}
</syntaxhighlight>
|