我正在调用一个函数,该函数返回一个整数,表示16个二进制输入的位域,每个颜色都可以打开或关闭。
我正在尝试创建一个函数来获取oldstate和new状态之间的变化,
e.g。
function getChanges(oldColors,newColors)
sampleOutput = {white = "",orange="added",magenta="removed" .....}
return sampleOutput
end
我尝试从newColors中减去oldColors,从oldColors中减去新颜色,但这似乎导致混乱应该超过1个值。
这是为了检测多个输入的上升沿/下降沿。
* *修改:似乎有subset of the lua bit api available
colors.white 1 0x1 0000000000000001
colors.orange 2 0x2 0000000000000010
colors.magenta 4 0x4 0000000000000100
colors.lightBlue 8 0x8 0000000000001000
colors.yellow 16 0x10 0000000000010000
colors.lime 32 0x20 0000000000100000
colors.pink 64 0x40 0000000001000000
colors.gray 128 0x80 0000000010000000
colors.lightGray 256 0x100 0000000100000000
colors.cyan 512 0x200 0000001000000000
colors.purple 1024 0x400 0000010000000000
colors.blue 2048 0x800 0000100000000000
colors.brown 4096 0x1000 0001000000000000
colors.green 8192 0x2000 0010000000000000
colors.red 16384 0x4000 0100000000000000
colors.black 32768 0x8000 1000000000000000
(这里应该有一个值表,但是我无法解决markdown的语法,看起来stackoverflow会忽略标准的html部分。)
答案 0 :(得分:3)
function getChanges(oldColors,newColors)
local added = bit.band(newColors, bit.bnot(oldColors))
local removed = bit.band(oldColors, bit.bnot(newColors))
local color_names = {
white = 1,
orange = 2,
magenta = 4,
lightBlue = 8,
yellow = 16,
lime = 32,
pink = 64,
gray = 128,
lightGray = 256,
cyan = 512,
purple = 1024,
blue = 2048,
brown = 4096,
green = 8192,
red = 16384,
black = 32768
}
local diff = {}
for cn, mask in pairs(color_names) do
diff[cn] = bit.band(added, mask) ~= 0 and 'added'
or bit.band(removed, mask) ~= 0 and 'removed' or ''
end
return diff
end