RosettaCodeData/Task/Gray-code/Pluto/gray-code.pluto

20 lines
401 B
Text
Raw Permalink Normal View History

2026-04-30 12:34:36 -04:00
local fmt = require "fmt"
local function to_gray(n) return n ~ (n >> 1) end
local function from_gray(g)
local b = 0
while g != 0 do
b ~= g
g >>= 1
end
return b
end
print("decimal binary gray decoded")
for b = 0, 31 do
fmt.write(" %2d %05d", b, fmt.bin(b))
local g = to_gray(b)
fmt.print(" %05d %05d", fmt.bin(g), fmt.bin(from_gray(g)))
end