peiyoke
asked on
<<|>>Bit packing
Hi,
I am trying to pack 4 6 bits data into 3 bytes. How do I check whether the packing that I have done is correct? I don't want to write the codes for unpacking the data before checking that the codes for packing is correct. Following is my code segment :
int packbit (unsigned char *a, int n, unsigned char *b)
{
int i;
int loop=n/4;
for(i=0; i<loop;a+=4,b+=3)
{
b[0]=a[0]+(unsigned char)(a[1]<<6);
b[1]=(a[1]>>2)+(unsigned char)(a[2]<<4);
b[2]=(a[2]>>4)+(unsigned char)(a[3]<<2);
}
}
Pls help with some code examples. Thanks.
Rgds,
PY
I am trying to pack 4 6 bits data into 3 bytes. How do I check whether the packing that I have done is correct? I don't want to write the codes for unpacking the data before checking that the codes for packing is correct. Following is my code segment :
int packbit (unsigned char *a, int n, unsigned char *b)
{
int i;
int loop=n/4;
for(i=0; i<loop;a+=4,b+=3)
{
b[0]=a[0]+(unsigned char)(a[1]<<6);
b[1]=(a[1]>>2)+(unsigned char)(a[2]<<4);
b[2]=(a[2]>>4)+(unsigned char)(a[3]<<2);
}
}
Pls help with some code examples. Thanks.
Rgds,
PY
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Even though I store my data as unsigned char, I came across a book which states that integral promotions are performed when shift expressions are evaluated. Does this means that even though my data are packed from 4 bytes to 3 bytes for the array elements, but the overall storage required to store the data has been increased by 4?
Can I or should I do some casting such as
(unsigned char)(a[0]>>2)?
Pls advise. Thanks.