LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-18-2010, 04:08 AM   #1
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
Bit packing


Suppose there is a number 1010 1101 0011

I want to set to 0, all the bits after the first 3 ones.

If I AND the following two numbers, I'll solve my problem

1010 1101 0011
&
1010 0000 0000
=
1010 0000 0000

Question:
How to generate the number 1010 0000 0000 ?

It is not clicking me somehow, how to use AND, OR and shift operations to get that number !
 
Old 03-18-2010, 04:36 AM   #2
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 447Reputation: 447Reputation: 447Reputation: 447Reputation: 447
Hi

When you "and" to clear bits, use 1 for all the bits you want to keep and 0 for all the others.

1010 1101 0011
&
1110 0000 0000
 
1 members found this post helpful.
Old 03-18-2010, 04:55 AM   #3
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731

Original Poster
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
Thanks for answering !

Now I am thinking how to generalize it.
i.e.

If user specifies

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 ?
 
Old 03-18-2010, 05:04 AM   #4
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731

Original Poster
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
And I think I have got it now:

3 : 1 + 2 will result in 111
4 : 1 + 2 + 4 + 8 will result in 1111
5 : 1 + 2 + 4 + 8 + 16 will result in 11111

See all the numbers I am adding are the multiples of 2.

Last edited by Aquarius_Girl; 03-18-2010 at 05:06 AM.
 
Old 03-18-2010, 05:05 AM   #5
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 447Reputation: 447Reputation: 447Reputation: 447Reputation: 447
The formula will be (2^number)-1

2^3-1=7
2^4-1=15
...
 
1 members found this post helpful.
Old 03-18-2010, 05:08 AM   #6
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731

Original Poster
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
Thanks again!

I think your and my formula are same ! Aren't they ?
 
Old 03-18-2010, 05:19 AM   #7
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731

Original Poster
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
Your formula is easier to implement than mine, thanks once again
 
Old 03-19-2010, 01:35 AM   #8
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731

Original Poster
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
This question is also related to the bit packing, therefore in the same thread

<content deleted>

Last edited by Aquarius_Girl; 03-19-2010 at 06:04 AM.
 
Old 03-19-2010, 02:13 AM   #9
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Hi,

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'?
 
Old 03-19-2010, 03:41 AM   #10
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731

Original Poster
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
<content deleted>

Last edited by Aquarius_Girl; 03-19-2010 at 06:04 AM.
 
Old 03-19-2010, 04:57 AM   #11
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731

Original Poster
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
Quote:
Originally Posted by crts
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.
 
Old 04-01-2010, 01:33 AM   #12
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731

Original Poster
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
Unpacking the packed bits

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!!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How can we create .bin file for packing the software. kmurthy7 Linux - Distributions 2 09-10-2008 08:08 AM
C++: Packing buffers for TCP carcassonne Programming 1 07-13-2006 02:41 AM
vcdimager + problem packing a video kurrupt Linux - Software 0 08-23-2005 12:54 PM
QMail packing a wobbly still AMMullan Linux - Software 2 06-11-2004 09:36 AM
Packing Customise data in Linux sachinnshah Linux - Laptop and Netbook 0 12-11-2003 07:42 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:26 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration