Bit OR operator problem in Lua

Hi Guys im porting some lib from JavaScript to roblox lua

and having trouble with bit operators

-- JS console.log((7/3) | 0); // = 2
local a = bit32.bor((7/3), 0)
print(a)
-- = input:2: bad argument #1 to 'bor' (number has no integer representation)

Lua in constrast to JavaScript doesn’t allow Floats for OR operator :(, how can I fix that?

2 Likes

Is there a specific case where you need bitwise floating-point OR operations? The library you are trying to port probably does not need to be implemented specifically with these type of operations. If you really need it/are unable to re-implement it in another way, you are going to need to evaluate the OR operation on your own converted bit array.

| 0 is a trick in JavaScript used to coerce numbers into integers (e.g. 7/3 would be 2.333…, (7/3) | 0 retrieves the integer part, which is 2). So all you need to do to port x | 0 is round x some other way (be careful with negative numbers! JS -1.5 | 0 = -1, Lua math.floor(-1.5) = -2)

3 Likes

(math.modf(x)) can be used to correctly retrieve the integer part of a number.

3 Likes

Turns our it’s always integer, so never mind :smiley: