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
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
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.
i manage to make something like this :
can i make it a little shorter? or write another code about it?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; }
thanks a lot
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.
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
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.
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
any ideas?
>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:
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.Code:if (onesCounter == 5){ if (--outputBit == 0) { outputBit = 8; outputByte++; output_ptr[outputByte] = 0; } onesCounter = 0; }
is thatsame with thatCode:bitvalue = (currentBit == 0) ? 0 : 1;are those the same?Code:if (currentbit==0) bitvalue=0 else bitvalue=1
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.
because i have other commands before the if and other commands aftershould i put that :Code:bitvalue=1
Code:if (currentbit==0) { bitvalue=0 else bitvalue=1 }
You'll find that won't compile.
Quzah.
Hope is the first step on the road to disappointment.