Lua xor 函数代码

这段代码在 Lua 中是否起到 xor 函数的作用?

function xor (a,b)
if a ~= b then return true else return false end
end

i = false
j = false
if xor(not i, not j) then
  print("one exclusive")
else
  print("both or none")
end
点赞
用户107090
用户107090

是的,你的代码可用。

如果 ab 包含 boolean 类型的值,那么 a XOR b 就等同于 not(a == b),显然也等同于 a ~= b

2018-06-08 15:07:17