shreya
shreya

Reputation: 407

Bit Packaging in C

I have gone through the available questions on Stack overflow but I did not find any relevant to my problem. I have image which contains binary data, each pixel I access as a byte but as my image binary I can release the memory using bit packing. But I don't know how to do it and I'm not finding any good sources to learn from.

Can anybody help me out?

Upvotes: 1

Views: 125

Answers (1)

unwind
unwind

Reputation: 399703

Not sure what references you found ... Or what the actual problem is.

You can do something like this:

typedef enum  { ZERO, ONE, TWO, THREE } pix2;

uint8_t pack_values(pix2 p1, pix2 p2, pix2 p3, pix4 p4)
{
  return (p1 << 6) | (p2 << 4) | (p3 << 2) | p4;
}

The above will "pack" four two-bit values into a single 8-bit value. Assigning the bits like this (excuse my ASCII graphics skills):

       +-+-+-+-+-+-+-+-+
bit:   |7|6|5|4|3|2|1|0|
       +-+-+-+-+-+-+-+-+
value: | p1| p2| p3| p4|
       +---+---+---+---+

Upvotes: 3

Related Questions