Basic Bit Operation Question.

T

tmp123

Ali said:
Quite interesting discussion here , yeah Alex is right
and Jhon must be having trouble with this method. I remember doing it
for MCU and

With method? Doing what?
that really sucks because of less memory

Taken into account that there are 3 proposals (and the posts ancestors
of your post), where the first one uses 3 vars, and the other two only
one, you talk about computers with very very few memory (even with few
registers).
 
K

Keith Thompson

Ali said:
Then what the heck you are doing if not sniping.

Snipping, not sniping. Perhaps you didn't understand what I meant.

Look at the top of this followup. The line that says


is an attribution line. You consistently delete (i.e., snip) those
lines when you post followups, making it difficult to see who wrote
what.

For example, Flash Gordon wrote the line starting "Keith requested ...",
but anyone reading your followup wouldn't know that.

Once again, please don't snip attribution lines.
 
A

Ali

Ok guys lets get back to the OP. I have seen that solutions posted here
were for certain values or just transforming the given value to
manchester code.
I assume that Jhon wants to use *SPI protocol which requires a single
bit by bit addressing. [See the code from Jhon's post]
Following lines of code will transform any given value to manchester
code and this is what OP was looking for. Though jhon has to take care
of timing problem according to his machine or mcu if bitrate is
critical for application.

Regards
ali

*SPI - The Serial Peripheral Interface is a general-purpose
synchronous serial interface originally found on certain Motorola
microcontrollers. A virtually identical interface can now be found on
various other microcontrollers as well.
[http://www.mmca.org/technology/glossary/]



#define bit_get(p,m) ((p) & (m))
#define BIT(x) (0x01 << (x))

int count =0;

void Out(char ch){

if(count++ % 4 == 0 )
printf(" %c" ,ch);
else
printf("%c" , ch);

}//Out
int main(){
//Here in array put what ever data you want , my machine was having 16
bit for unsigned int. If your value is greater than this then you need
some other data type;-)

unsigned int data[] = {0x00 , 0x33a , 0x00 , 0x12 , 0x01 , 0x0f , 0x00
, 0x00, 0x00 , 0x00 , 0x00 , 0x00 , 0x00 };

int manchester;
int bitcounter;
int i;

for(i =0 ; i <= 7; i++){
switch(i){
case 0: { bitcounter = 8; printf(" \n0x00 = "); break; }//
case 1: { bitcounter = 9; printf(" \n0x33a = "); break;
}//delimiter
case 2: { bitcounter = 7; printf(" \n0x00 = "); break; }//
case 3: { bitcounter = 7; printf(" \n0x12 = "); break; }//
case 4: { bitcounter = 7; printf(" \n0x01 = "); break; }//
case 5: { bitcounter = 7; printf(" \n0x0f = "); break; }//
case 6: { bitcounter = 27; printf(" \nWordData 7 bytes 0x00 =
"); break; }//
case 7: { bitcounter = 27; printf(" \nWordData 7 bytes 0x00 =
"); break; }//

}//switch

for(; bitcounter != -1; bitcounter--){

if(bit_get(data, BIT((bitcounter))) & 0xff )
{
Out('1');
Out('0');

}
else
{
Out('0');
Out('1');

}

}//bitcounter != -1

count=0;
}//for(i =0 ; i < 7; i++)

printf("\n \n");
return 0;
}//main
 
A

Ali

Oops! can see a blunder in my code;-)
Yeah case 6 and 7 are using 27 for sentinel values where as data type
is just 16 bit. So do accecpt my apologies.
I think using an array instead of switch for sentinel value can improve
the performance.

Regards.
ali
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top