cencodingmemory-optimization

How to encode list of 16 numbers in less than 2 bytes


I need to convey information of availability of 16 items with their id(0-15) in a variable.

char item_availablity[16];

I can encode it with 2 bytes where every bit is mapped with one item id where 1 represents available and 0 represents unavailable For ex 0000100010001000 This number has information that Items with id 4,8,12 are available

I need to encode this information by using less than 2 Bytes.

Is this possible? If so, how?


Solution

  • To put it simply:

    No, you cannot. It's simply impossible to store 1 bit of information about 16 separate things in less than 16 bits. That is, in the general case.

    An exception is if there are some restrictions. For instance, let's call the items i_1, i_2 ... i_16. If you know that i_1 is available if and only if i_2 also is available, then you can encode the availability about these two items in just one bit.

    A more complicated example is that i_1 is available iff i_2 or i_3 is available. Then you could store the availability for these three items in two bits.

    But for the general case, nope, it's completely impossible.