23.03.2022, 09:50
Use this function, it returns a number that is stored in bits starring from offset to offset + length (not inclusive). offset is the lowest bit number that you want to get. length is number of bits to get.
Note that bits are counted from right to left starting from 0. So bits 9..11 are this: 0001 0011 1000 1000
Code:
function getbits(value, offset, length)
local mask = bit.lshift(1, length) - 1
return bit.band(bit.rshift(value, offset), mask)
end
value = 5000
result = getbits(value, 9, 3)
log(result)
Note that bits are counted from right to left starting from 0. So bits 9..11 are this: 0001 0011 1000 1000