RosettaCodeData/Task/Gray-code/11l/gray-code.11l

16 lines
314 B
Text
Raw Permalink Normal View History

2023-07-01 11:58:00 -04:00
F gray_encode(n)
R n (+) n >> 1
F gray_decode(=n)
V m = n >> 1
L m != 0
n (+)= m
m >>= 1
R n
print(DEC, BIN => GRAY => DEC)
2026-02-01 16:33:20 -08:00
L(i) 0.<32
2023-07-01 11:58:00 -04:00
V gray = gray_encode(i)
V dec = gray_decode(gray)
print( #2, #. => #. => #2.format(i, bin(i).zfill(5), bin(gray).zfill(5), dec))