20 lines
401 B
Text
20 lines
401 B
Text
|
|
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
|