Thread: bit stuffing

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    8

    bit stuffing

    hello guys...can someone help me with bit stuffing technique?
    i want to send packet from one device to another, i read bytes per bytes then i develop bit stuffing and the result goes to a pointer that saw to an address

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    So all you need is some application of << >> & and | operators to shuffle bits to the right position, then add them (or test for them) within a larger storage unit.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8
    i manage to make something like this :

    Code:
    int bit_stuffing(unsigned char * input_ptr, unsigned char * output_ptr, unsigned short bytesNum){
    
    	unsigned char currentBit;
    	int bitvalue;
    	int inputByte, inputBit, outputByte, outputBit;
    	int onesCounter;
    
    	inputByte = outputByte = 0;
    	onesCounter = 0;
    	outputBit = 8; 
    	output_ptr[outputByte] = 0;
    	for (inputByte = 0 ; inputByte < bytesNum ; inputByte++) {
    		unsigned char mask;
    		for (mask = 0x80 ; mask > 0 ; mask >>= 1){
    			currentBit = mask & input_ptr[inputByte];
    			bitvalue = (currentBit == 0) ? 0 : 1; 
    			onesCounter = (currentBit == 0) ? 0 : (onesCounter + 1); 
    			output_ptr[outputByte] |= bitvalue << (outputBit - 1); 
    			if (--outputBit == 0) { 
    				outputBit = 8;
    				outputByte++;
    				output_ptr[outputByte] = 0;
    			}
                if (onesCounter == 5){
    				if (--outputBit == 0) { 
    					outputBit = 8;
    					outputByte++;
    					output_ptr[outputByte] = 0;
    				}
    				onesCounter = 0;
    			}
    		}
    	}
    	return outputByte;
    }
    can i make it a little shorter? or write another code about it?
    thanks a lot

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Before worrying about making it shorter, does it work?

    Have you measured how fast it is, and is that quick enough?

    Yes, you could probably make it shorter, possibly quite a lot shorter and all pretence of readability and maintainability would disappear along with it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8
    i understand what u mean...yes that code is working...the point is i manage to make that code with a friend and now i want to make it in another way, so maybe i can change the sequence of functions and those..and if i change the sequence maybe it will work faster

  6. #6
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Profiling is something you are wanting asxetos... Take this, and use it many many times with random input... see if that is satisfactory in speed... if not, start taking pieces of this function and testing to see if you can make any part of it run faster... often times the speed it takes to do a function once is too small to be measured reliably. So the solution is running it a couple hundred or thousand times and recoding that time.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8
    i understand what u mean, cause i am not so experience in c, maybe some more experience person can help me in that?
    thanks a lot

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8
    any ideas?

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >any ideas?
    Perhaps you need to explain what you want the function to do. Show us sample input, and the expected output. Near as I can tell, it simply takes the input array and transfers it to the ouput array. The only part that's doing any transformation is here:
    Code:
    	if (onesCounter == 5){
    		if (--outputBit == 0) { 
    			outputBit = 8;
    			outputByte++;
    			output_ptr[outputByte] = 0;
    		}
    		onesCounter = 0;
    	}
    What you have already is probably pretty fast, but you can use the clock function to time it, or as has been suggested, a profiler.

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8
    is that
    Code:
    bitvalue = (currentBit == 0) ? 0 : 1;
    same with that
    Code:
     if (currentbit==0)
    bitvalue=0
    else 
    bitvalue=1
    are those the same?

  11. #11
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Yes, those are the same.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    8
    because i have other commands before the if and other commands after
    Code:
    bitvalue=1
    should i put that :
    Code:
    if (currentbit==0)
    {
    bitvalue=0
    else 
    bitvalue=1
    }

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You'll find that won't compile.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 11-10-2005, 10:53 AM
  2. porting application from 32 bit to 64 bit error
    By gandalf_bar in forum Linux Programming
    Replies: 1
    Last Post: 09-14-2005, 09:20 AM
  3. Bit processing in C
    By eliomancini in forum C Programming
    Replies: 8
    Last Post: 06-07-2005, 10:54 AM
  4. Bit Manipulation Questions
    By CPPNewbie in forum C++ Programming
    Replies: 7
    Last Post: 08-12-2003, 02:17 PM
  5. Array of boolean
    By DMaxJ in forum C++ Programming
    Replies: 11
    Last Post: 10-25-2001, 11:45 PM