Logic Gate Library

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 = true
false, true = true
true, false = true
true, true = false

nor

false, false = true
false, true = false
true, false = false
true, true = false

xnor

false, false = true
false, true = false
true, false = false
true, true = true

xor

false, false = false
false, 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
Example using an Instance as input:
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
Module link and source:

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}
9 Likes

Don’t we already just have and or not and ~=

1 Like

Yes, but they aren’t the only logic gates that exist:

1 Like

Could you give an example of the module in an actual use case scenario? Not just a print

1 Like
print(logic.nand(false, false)) -- true
print(logic.nor(false, true))   -- false
print(logic.xnor(true, false))  -- false
print(logic.xor(true, true))    -- false
print(false == false) -- true
print(false and true) -- false
print(true and false) -- false
print(true ~= true)   -- false

:man_shrugging:

2 Likes

Yeah, all of this is possible just within roblox.

Cool module I guess, although it is only needed for rare cases, and where most of the time developers just use a combination of the 3 branch words for these rare cases.

Also for anyone who needs this, I’ve made modules such as simpleBit v2 to make the usage of these more visually appealing and have increased functionality.

As an example for logic.xor:

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local logic = require(ReplicatedStorage.Logic)

local condition1 = true
local condition2 = true

if logic.xor(condition1, condition2) then
	-- The code here will only run if only 1 condition is true
else
	-- The code here will run if either both conditions are true or false
end

In the end, whether or not you need to use my module depends on your personal programming style. If you’re satisfied with the logic operators that are already present then continue to use them

Also the bit32 library can be used in this, and in order to take multiple inputs at once. The version below takes inputs as true represented as 1 and false represented as 0, but these can easily be decoded with 1 line of code to and from boolean:

local EXT = bit32.extract
local BXOR = bit32.bxor

local function xor(...:number)
	return EXT(BXOR(...), 0)
end

The benefit of using my module is readability, really. If you ever encounter a situation where you need to use a logic gate that’s not currently present it makes it more obvious what your intention was

The bit32 library deals with number values, my library is meant to mimic the behavior of the existing operators so it accepts any type of input and always returns a boolean result

1 Like

The current logic gates just add more work than reducing it, Luau already has shortcuts

It would’ve been cool if there are logic gates that Luau doesn’t have

1 Like

I’m aware that Luau already has the and, not & or gates present, which is why my module adds the nand, nor, xnor & xor gates which are currently absent (yes the bit32 library has xor, but it deals with number values instead of working similar to the logic operators)

I don’t understand the reason as to why people are assuming I simply re-added already existing logic gates, please do your best to read the whole topic before making assumptions since I explained here which gates my library includes:

You can even see in the source which gates are included in my module, I don’t have a function for and, not & or since they aren’t necessary as they are already included and it would only result in an extra function call if I did add them to it, so I willingly and intentionally left them out

It’s very rude to comment before first understanding the contents of the topic, and I’m willing to help explain to anyone who has any difficulty doing so, but please be cautious first