A library that contains functions which accurately simulate the behavior of logic gates currently absent in Luau and Lua. This library is intended to replicate the use case scenario of the existing and
, not
& or
logic operators, so for bitwise operations please use the bit32 library instead
The gates supported are:
nand
false, false = truefalse, true = true
true, false = true
true, true = false
nor
false, false = truefalse, true = false
true, false = false
true, true = false
xnor
false, false = truefalse, true = false
true, false = false
true, true = true
xor
false, false = falsefalse, true = true
true, false = true
true, true = false
Usage example:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local logic = require(ReplicatedStorage.Logic)
print(logic.nand(false, false)) -- true
print(logic.nor(false, true)) -- false
print(logic.xnor(true, false)) -- false
print(logic.xor(true, true)) -- false
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local logic = require(ReplicatedStorage.Logic)
local part = Instance.new("Part")
print(logic.nand(nil, nil)) -- true
print(logic.nor(nil, part)) -- false
print(logic.xnor(part, nil)) -- false
print(logic.xor(part, part)) -- false
Every function is guaranteed to return a boolean value as a result, no matter the types of the input values. This is done to mimic the behavior of the existing logic operators as closely as possible, as I previously stated
Logic Gate Library on the Creator Marketplace
--!strict
return table.freeze{
nand = function(a: any, b: any): boolean
return not (a and b)
end,
nor = function(a: any, b: any): boolean
return not (a or b)
end,
xnor = function(a: any, b: any): boolean
if a and b then return true end
return not (a or b)
end,
xor = function(a: any, b: any): boolean
if a and b then return false end
return (a or b) and true or false
end}