Solved

<<|>>Bit packing

Posted on 1998-09-18
2
409 Views
Last Modified: 2010-04-15
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
0
Comment
Question by:peiyoke
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 4

Accepted Solution

by:
emmons earned 10 total points
ID: 1252874
First, you have a iterator in the for loop that is never incremented, that causes something of a problem when the code is run.
Second, you seem to be trying to make the pack routine more general than the description in your message, and yet there is an assumption in the code that it is always packing 4 bytes into 3.

To do the conversion, try
      b[0] = ((a[0] << 2) & 0xFC) + ((a[1] >> 4) & 0x03);
      b[1] = ((a[1] << 4) & 0xF0) + ((a[2] >> 4) & 0X0F);
      b[2] = ((a[2] << 6) & 0xF0) + ((a[3] & 0x3F));

0
 

Author Comment

by:peiyoke
ID: 1252875
What I am trying to do is to compress data in a file and packing algorithm to be used is not restricted. After running the codes for packing, can I determine from output directly whether the codes is correct? I tried to examine the bit pattern of the output but don't see any correlation between the input and the output data.

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.

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

695 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question