ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
3, we will AND with 111, whereas 111 actually means 7
4, we will AND with 1111, whereas 1111 actually means 15
5, we will AND with 11111, whereas 11111 actually means 31
Now the task is how to make the program understand that when user has specified 3 we have to AND with 7 ... ?
I don't think making a mapping table will be a good idea, there must be some better way to do it ?
I am not sure if I understand your question. If your data is in an array (of element length 1 byte) then you could just XOR the last element with 1110 0000 or the first with 0000 0111. Not exactly sure what you mean with first three bits - MSB or LSB? And am I understanding right that in this context you want to interpret the 4096 byte as 'one huge number'?
I am not sure if I understand your question. If your data is in an array (of element length 1 byte) then you could just XOR the last element with 1110 0000 or the first with 0000 0111. Not exactly sure what you mean with first three bits - MSB or LSB? And am I understanding right that in this context you want to interpret the 4096 byte as 'one huge number'?
Thanks for bothering to reply !
I apologize for wasting your time, there was a communication gap between me and my senior, hence resulting in a wrong question, and now the question what I have understood is easily solvable !
Thanks again !
Last edited by Aquarius_Girl; 03-19-2010 at 06:08 AM.
I have successfully written the bit packing code and now comes the turn of unpacking it:
In the following code data is the name of the array that contains the packed bits.
Code:
void packedMessage :: getUnpackedMsg (unsigned char *emptyArray, unsigned int numberOfBits)
{
unsigned int bitsPending = numberOfBits;
unsigned int andWith = 0;
unsigned int andResult = 0;
// Calculating the number of bytes
int remainder = numberOfBits % 8;
int quotient = numberOfBits / 8;
unsigned short numberOfBytes = quotient;
if (remainder > 0)
{
numberOfBytes++;
}
unsigned int i = 0;
while (numberOfBytes > 0)
{
if (numberOfBytes == 1)
{
andWith = (pow (2, bitsPending)) - 1;
andResult = data[index] & andWith;
emptyArray[i] = emptyArray[i] | andResult;
--index;
}
else if ((numberOfBytes > 1) && (bitsPending >= 8))
{
emptyArray[i] = emptyArray[i] | data[index];
bitsPending = bitsPending - 8;
++i;
--index;
}
--numberOfBytes;
}
printf ("\nAfter unpacking: %d ", emptyArray[0]);
printf ("\nAfter unpacking: %d ", emptyArray[1]);
cout << "\n";
}
Main function from which I am calling the above
Code:
int main ()
{
// Test case : 2 bytes 11 bits
int byte = 2047;
packedMessage p;
p.getPackedMsg ((unsigned char*)&byte, 11);
p.setDataArray ((unsigned char*)&byte, 11);
unsigned int *empty = new unsigned int [5];
p.getUnpackedMsg ((unsigned char*)&empty, 11);
Question:
How I am supposed to print what ever I have unpacked, Those print statements in the unpack function do not seem to work properly !
If my question is not clear please let me know !
Help kindly!!
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.