As a Roblox developer, it is currently difficult to make expressions that utilise chains of bitwise operators look clean.
The reasoning is because to do any bitwise operation, we need to defer to the bit32 library, rather than being able to use mathmatical equations.
Lets take a simple algorithm, decoding a ULEB128 number from a binary stream, in most high level languages, we can use the operators for bitwise AND and bitwise shifting to create this nice, clean and readable algorithm.
const decodeSignedLeb128 = (input) => {
let result = 0;
let shift = 0;
while (true) {
const byte = input.shift();
result |= (byte & 0x7f) << shift;
shift += 7;
if ((0x80 & byte) === 0) {
if (shift < 32 && (byte & 0x40) !== 0) {
return result | (~0 << shift);
}
return result;
}
}
};
But in Luau, we need to make calls to the bit32 library, which results in this much noisier function:
local function decodeSignedLeb128(b: buffer, offset: number): number
local result, shift = 0, 0
while true do
local byte = buffer.readu8(b, offset)
result = bit32.bor(result, bit32.lshift(bit32.band(byte, 0x7F), shift))
shift += 7
offset += 1
if bit32.band(byte, 0x80) == 0 then
if shift < 32 and bit32.band(byte, 0x40) ~= 0 then
return bit32.bor(result, bit32.lshift(bit32.bnot(0), shift))
end
return result
end
end
end
there may be logic errors here, using this as an example
As you can see, especially with result
, we had to chain a massive statement of bit32 calls, to do something that is a lot cleaner with operators.
I’m also requesting this because it would aid in porting file parsers from other languages that run under the assumption that bitwise operators exist, and its less hassle to unwind the order of operations if I can create a 1:1 mapping with equivalent operators.
If Roblox were to address this issue it would improve my development experience because it would result in cleaner code when working with bitwise operators, and make it easier to port code from other languages.