What do you want to achieve? Keep it simple and clear!
Switch out the bit32 library for native operators (such as x | y instead of bit32.bor(x, y))
What is the issue? Include screenshots / videos if possible!
Multiple bit32 functions take up too much space and are slow to type
What solutions have you tried so far? Did you look for solutions on the Creator Hub?
Making my own functions with shorter names (such as rs(x, m) for bit32.rshift(x, m))
Implementing Lua 5.3’s bitwise operators could solve this (using something like 16>>1 instead of bit32.lshift(16,1) saves 13 whole characters and removes a lot of visual clutter)
Just make local variables and reuse them in your code.
For example:
local bor = bit32.bor
--now it uses six characters less
print(bor(x, y))
Also if I am not mistaken you can use x * 2 and x // 2 to shift a number by specific bits (for more just change the power of two at the second part). Although the above can have slightly different behavior for certain cases, such as really large numbers.
The example 16>>1 is a right shift, so its equivalent is bit32.rshift, not bit32.lshift..
Luau has no 64 bit integer type. bit32 is more fully featured, with rotates, arithmetic shift, and bit extraction and replacement. Performance is better with bit32 because it uses VM builtins rather than expensive function calls. Operators would need metamethods for each of them, ugg. AND the caret is not available as an xor operator, and arithmetic right shift can’t be expressed without yet another operator..
You could always do this..
-- ServerScript ServerScriptService
local bor = bit32.bor
local band = bit32.band
local bxor = bit32.bxor
local rshift = bit32.rshift
local lshift = bit32.lshift
local flags = bor(lshift(1, 3), 1)
local highBits = rshift(flags, 1)
local isSet = band(flags, 8) ~= 0
Module
-- BitFlags
-- ModuleScript
local BitFlags = {}
local bor = bit32.bor
local band = bit32.band
local bxor = bit32.bxor
local bnot = bit32.bnot
local rshift = bit32.rshift
local lshift = bit32.lshift
function BitFlags.new()
return 0
end
function BitFlags.set(flags, bit)
return bor(flags, lshift(1, bit))
end
function BitFlags.clear(flags, bit)
return band(flags, bnot(lshift(1, bit)))
end
function BitFlags.toggle(flags, bit)
return bxor(flags, lshift(1, bit))
end
function BitFlags.has(flags, bit)
return band(flags, lshift(1, bit)) ~= 0
end
function BitFlags.get(flags, bit)
return band(rshift(flags, bit), 1)
end
return BitFlags