It looks like lua fills vacant bits with 0 regardless, whereas JavaScript will fill vacant bits with the sign bit (something to do with two’s complement, I’m not very sure)

Maybe I read the documentation wrong, perhaps you need to use a loop instead, but the base premise is to replace bits with a different bit so that a negative number is bit-shifted with 1s instead of 0s

turns out lua just decides that the integer becomes unsigned or something, but this code snippet seems to do the trick:

local function rshiftsigned(num: number, shiftAmount: number)
local shiftedNum = bit32.rshift(num, shiftAmount)
if num < 0 then
return shiftedNum - 2 ^ 32
else
return shiftedNum
end
end

You can do that with ro-typescript for question 1. For 2 it shows that the bit32 library will absolute your input causes your binary thing looks like this 10000000 and when you right shift it in lua you get essentially a really high number (2^32-1). For javascript, it’s probably cause it uses left shift if the input is negative cause it makes no sense to shift bits by a negative number. right shift and left shift actually just execute a couple bitmasks and AND operations

local function alshift(x: number, disp: number): number
local y = bit32.lshift(x, disp)
if y >= 2 ^ 31 then
return y - 2 ^ 32
end
return y
end
local function arshift(x: number, disp: number): number
local y = bit32.rshift(x, disp)
if y >= 2 ^ 15 then
return y - 2 ^ 16
end
return y
end