RosettaCodeData/Task/Gray-code/Nim/gray-code-1.nim
2023-07-01 13:44:08 -04:00

9 lines
163 B
Nim

proc grayEncode(n: int): int =
n xor (n shr 1)
proc grayDecode(n: int): int =
result = n
var t = n
while t > 0:
t = t shr 1
result = result xor t