omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
omega-rosetta / tasks / binary-digits / binary-digits.om
| 1 | note Rosetta Code task "Binary digits": https://rosettacode.org/wiki/Binary_digits |
| 2 | note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | om2 |
| 4 | text nl "\n" |
| 5 | note emit writes every byte of a to stdout; a short write is retried, an error exits 1 |
| 6 | fn emit a view uchar * to void |
| 7 | o = ulong 0 |
| 8 | while lt o len a bound 4096 |
| 9 | k = write 1 a o sub len a o |
| 10 | if le k 0 |
| 11 | exit 1 |
| 12 | end |
| 13 | o = add o ulong k |
| 14 | end |
| 15 | end |
| 16 | fn bin n ulong to void |
| 17 | d = new uchar 64 0 |
| 18 | i = ulong 64 |
| 19 | v = n |
| 20 | while true bound 64 |
| 21 | i = sub i 1 |
| 22 | put d i uchar add and v 1 48 |
| 23 | v = shr v 1 |
| 24 | if eq v 0 |
| 25 | break |
| 26 | end |
| 27 | end |
| 28 | emit part d i sub 64 i |
| 29 | emit nl |
| 30 | end |
| 31 | fn main to int |
| 32 | bin 0 |
| 33 | bin 5 |
| 34 | bin 50 |
| 35 | bin 9000 |
| 36 | bin 18446744073709551615 |
| 37 | ret 0 |
| 38 | end |