Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
18
Task/Gray-code/Lua/gray-code-1.lua
Normal file
18
Task/Gray-code/Lua/gray-code-1.lua
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
local _M = {}
|
||||
|
||||
local bit = require('bit')
|
||||
local math = require('math')
|
||||
|
||||
_M.encode = function(number)
|
||||
return bit.bxor(number, bit.rshift(number, 1));
|
||||
end
|
||||
|
||||
_M.decode = function(gray_code)
|
||||
local value = 0
|
||||
while gray_code > 0 do
|
||||
gray_code, value = bit.rshift(gray_code, 1), bit.bxor(gray_code, value)
|
||||
end
|
||||
return value
|
||||
end
|
||||
|
||||
return _M
|
||||
24
Task/Gray-code/Lua/gray-code-2.lua
Normal file
24
Task/Gray-code/Lua/gray-code-2.lua
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
local bit = require 'bit'
|
||||
local gray = require 'gray'
|
||||
|
||||
-- simple binary string formatter
|
||||
local function to_bit_string(n, width)
|
||||
width = width or 1
|
||||
local output = ""
|
||||
while n > 0 do
|
||||
output = bit.band(n,1) .. output
|
||||
n = bit.rshift(n,1)
|
||||
end
|
||||
while #output < width do
|
||||
output = '0' .. output
|
||||
end
|
||||
return output
|
||||
end
|
||||
|
||||
for i = 0,31 do
|
||||
g = gray.encode(i);
|
||||
gd = gray.decode(g);
|
||||
print(string.format("%2d : %s => %s => %s : %2d", i,
|
||||
to_bit_string(i,5), to_bit_string(g, 5),
|
||||
to_bit_string(gd,5), gd))
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue