I have 4 variable between 0~0x1FF(9-bit)
and 1 variable between 0~0xF(4-bit)
and I want to pack them into binary. As usual, I will pack the first four with pack('H', var)
(unsigned short, 16-bit), then pack the last one with pack('B', var)
(unsigned char, 8-bit).
so I will cost 72 bits
:
16 * 4 + 8 = 72
I am facing a special situation: my storage is very very precious, I just need 40 bits:
4 * 9 + 1 * 4
I waste 32 bits, and also these 32 bits can be important to me, because I have a lot of data to pack, every time I waste 32 bits, at last, my storage complains to me.
In fact, I can change 4 9-bit variable and 1 4-bit variable into 5 8-bit variable(unsigned char), then I pack them all with pack('B', var)
, of course I save 32 bits.
4 * 9 + 1 * 4 == 5 * 8
How can I simply change pack 4 9-bit variable and 1 4-bit variable int 5 8-bit variable?