Name: Anonymous 2014-01-28 23:42
does anyone have a working example of bit packing with negative numbers? I'm trying to pack three numbers, 8 bits each (from -255 to 255), by adding 255 to each number before I pack it, and just subtracting the same thing from it after I unpack. For some reason, I get weird ass errors around 0, like packing 0 but unpacking 256, and packing -1 and unpacking -256. I've tried everything I can think of and I have no idea what else to do.
local range = 8
local full = bit.lshift(1, range + 1) - 1
local half = bit.lshift(1, range) - 1
local a, b, c = -32, -64, -128
local x =
bit.bor(
bit.lshift( a + half, range*2 + 2),
bit.lshift( b + half, range + 1),
c + half
)
local f = bit.band( x, full ) - half
local e = bit.band( bit.rshift( x, range + 1), full) - half
local d = bit.rshift( x, range*2 + 2) - half