RosettaCodeData/Task/UTF-8-encode-and-decode/Jq/utf-8-encode-and-decode-1.jq
2023-07-01 13:44:08 -04:00

15 lines
440 B
Text

# input: a decimal integer
# output: the corresponding binary array, most significant bit first
def binary_digits:
if . == 0 then 0
else [recurse( if . == 0 then empty else ./2 | floor end ) % 2]
| reverse
| .[1:] # remove the leading 0
end ;
# Input: an array of binary digits, msb first.
def binary_to_decimal:
reduce reverse[] as $b ({power:1, result:0};
.result += .power * $b
| .power *= 2)
| .result;