I’m trying to optimize the amount of memory being sent to the client via bit packing. Its was going well but the “orientation” will either print 0 or 1 instead of its actual value when i try to extract it. The rest of the data extracts just fine though so how could this be?
local X = 54 -- max of 64
local Z = 46 -- max of 64
local Orientation = 176 -- max of 256
local ID = 1867 -- max of 4096
local XPosition_BITS = 0b0000_0011_1111_1111_1111_1111_1111_1111
local ZPosition_BITS = 0b1111_1100_0000_1111_1111_1111_1111_1111
local Orientation_BITS = 0b1111_1111_1111_0000_0000_1111_1111_1111
local ID_BITS = 0b1111_1111_1111_1111_1111_0000_0000_0000
local PositionBitPack = 0
PositionBitPack += bit32.band(X, XPosition_BITS)
PositionBitPack += bit32.band(bit32.lshift(Z, 6), ZPosition_BITS)
PositionBitPack += bit32.band(bit32.lshift(Orientation, 12), Orientation_BITS)
PositionBitPack += bit32.band(bit32.lshift(ID, 20), ID_BITS)
local x = bit32.extract(PositionBitPack, 0, 6)
print(x) -- prints 54
local y = bit32.extract(PositionBitPack, 6, 6)
print(y) -- prints 46
local orientation = bit32.extract(PositionBitPack, 12, 8)
print(orientation) --- prints... 1?, it should print 176
local id= bit32.extract(PositionBitPack, 20, 12)
print(id) --- prints 1867
What confuses me most is how “id” extracts perfect fine right after orientation. I played around with it more and it seems like it also prints 0 or 1 if there’s not enough bits allocated to actually store it. But 8 bits is more than enough. I believe the reason its not working here is maybe due to a syntax issue but im really not sure.
I mean, if its of any help I can get 176 to print but not 1867!
local X = 54 -- max of 64
local Z = 46 -- max of 64
local Orientation = 176 -- max of 256
local ID = 1867 -- max of 4096
local XPosition_BITS = 0b0011_1111
local ZPosition_BITS = 0b1111_1100_0000
local Orientation_BITS = 0b1111_1111_0000_0000_0000
local ID_BITS = 0b1111_1111_1111_0000_0000_0000
local PositionBitPack = 0
PositionBitPack += bit32.band(X, XPosition_BITS)
PositionBitPack += bit32.band(bit32.lshift(Z, 6), ZPosition_BITS)
PositionBitPack += bit32.band(bit32.lshift(Orientation, 12), Orientation_BITS)
PositionBitPack += bit32.band(bit32.lshift(ID, 24), ID_BITS)
local x = bit32.extract(PositionBitPack, 0, 6)
print(x) -- prints 54
local y = bit32.extract(PositionBitPack, 6, 6)
print(y) -- prints 46
local orientation = bit32.extract(PositionBitPack, 12, 8)
print(orientation) -- prints 176
local id = bit32.extract(PositionBitPack, 20, 12)
print(id) -- prints 1867
You need to reverse the order on your bit sequences and flip the 0s to 1s. You more or less got lucky that the other numbers made it through, but Orientation was right in the middle of the sequence and was being zeroed out.
local X = 54 -- max of 64
local Z = 46 -- max of 64
local Orientation = 176 -- max of 256
local ID = 1867 -- max of 4096
local XPosition_BITS = 0b0000_0000_0000_0000_0000_0000_0011_1111
local ZPosition_BITS = 0b0000_0000_0000_0000_0000_1111_1100_0000
local Orientation_BITS = 0b0000_0000_0000_1111_1111_0000_0000_0000
local ID_BITS = 0b1111_1111_1111_0000_0000_0000_0000_0000
local PositionBitPack = 0
PositionBitPack += bit32.band(X, XPosition_BITS)
PositionBitPack += bit32.band(bit32.lshift(Z, 6), ZPosition_BITS)
PositionBitPack += bit32.band(bit32.lshift(Orientation, 12), Orientation_BITS)
PositionBitPack += bit32.band(bit32.lshift(ID, 20), ID_BITS)
local x = bit32.extract(PositionBitPack, 0, 6)
print(x) -- prints 54
local y = bit32.extract(PositionBitPack, 6, 6)
print(y) -- prints 46
local orientation = bit32.extract(PositionBitPack, 12, 8)
print(orientation) -- prints 176
local id= bit32.extract(PositionBitPack, 20, 12)
print(id) --- prints 1867