Will we ever get native bitwise operators?

  1. 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))

  2. What is the issue? Include screenshots / videos if possible!
    Multiple bit32 functions take up too much space and are slow to type

  3. 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)

2 Likes

Unlikely.

1 Like

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.

no, one of the roblox staff addressed this already, they wont add it cause of the type solver

what are you cooking bro if it takes too much space?
I like at most stack 2 of them per line

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

educate yourself.

local a = 123i
local b = 1_000i
local c = 0xABABi
local d = 0b1000_1000i

here are your 64bit integers

1 Like