Jump to content
Sign in to follow this  
james :-)

"Reversing" a binary number?

Recommended Posts

Hi all,

 

Anybody got a quick and easy way to "reverse" a binary number?  I want to read an 8 bit port (B) and output a reversed byte (d7 - d0, d6 - d1, etc) to another port ©.

 

For example:

 

10011110 (port b, bits d0 - d7) input from port b...

 

01111001 (port c, bits d0 - d7) output to port c...

 

Before I start masking individual bits is there an easier way to do it?  My maths isn't the best!!

 

 

Best regards,

 

 

James Dexter :-)

Share this post


Link to post
Share on other sites

Perhaps this function can help you:

 

char swap_bits(char variable)

{

  char temp;

 

  temp = ((variable & 10000000b) >> 7) | ((variable & 00000001b) << 7);

  temp |= ((variable & 01000000b) >> 5) | ((variable & 00000010b) << 5);

  temp |= ((variable & 00100000b) >> 3) | ((variable & 00000100b) << 3);

  temp |= ((variable & 00010000b) >> 1) | ((variable & 00001000b) << 1);

 

  return temp;

}

Share this post


Link to post
Share on other sites
Guest Dave

char swap_bits(char variable)

{

 char temp;

 temp = 0;

 temp |= variable & 0x80 ? 0x01 ;

 temp |= variable & 0x40 ? 0x02 ;

 temp |= variable & 0x20 ? 0x04 ;

 temp |= variable & 0x10 ? 0x08 ;

 temp |= variable & 0x08 ? 0x10 ;

 temp |= variable & 0x04 ? 0x20 ;

 temp |= variable & 0x02 ? 0x40 ;

 temp |= variable & 0x01 ? 0x80 ;

 

 return temp;

}

 

I think doing it this way is much more clear.

 

Regards

Dave

Share this post


Link to post
Share on other sites
Guest Pavel

Now my turn B)

 

char swap_bits(char variable)
{
 char temp;
 
 asm rrf param00_swap_bits, F
 asm rlf _temp_swap_bits, F
 
 asm rrf param00_swap_bits, F
 asm rlf _temp_swap_bits, F
 
 asm rrf param00_swap_bits, F
 asm rlf _temp_swap_bits, F
 
 asm rrf param00_swap_bits, F
 asm rlf _temp_swap_bits, F
 
 asm rrf param00_swap_bits, F
 asm rlf _temp_swap_bits, F
 
 asm rrf param00_swap_bits, F
 asm rlf _temp_swap_bits, F
 
 asm rrf param00_swap_bits, F
 asm rlf _temp_swap_bits, F
 
 asm rrf param00_swap_bits, F
 asm rlf _temp_swap_bits, F
 
 return temp;
}

 

Regards,

Pavel

Share this post


Link to post
Share on other sites
Guest Dave

Admin,

 

I like it B)

 

Yours is the most effecient, but you had to slip down to ASM to get there.

 

Its does show the benefit of the asm inline code feature in PicAnt.

 

Regards

Dave

Share this post


Link to post
Share on other sites
Guest Dave

James,

 

Another alternative:

 

Change the hardware, so that the connection do the swap: move connection from RB0 to RB7,

move connection from RB1 to RB6,

move connection from RB2 to RB5,

move connection from RB3 to RB4,

move connection from RB4 to RB3,

move connection from RB5 to RB2,

move connection from RB6 to RB1,

move connection from RB7 to RB0,

 

Or swap the connections on the output port in the same way.

Then the programming overhead is zero. B)

 

Regards

Dave

Share this post


Link to post
Share on other sites
Guest Dave

How you didn't take it the wrong way.

 

In the past I have may it hardwork for myself like this.

 

I felt that the external D0 had to be connected RA0, D1 to RA1 and so on - it just looked write on the schematic.

 

So I thought I would suggest it just in case.

B)

Regards

Dave

Share this post


Link to post
Share on other sites

Hi Dave,

 

The data being read is bi-directional and sometimes I need to read it "straight" if you know what I mean.

 

Other times it reverses....nightmare!!

 

Mind you there is an active low enable I can read to know which way it needs to be read.

 

 

Best regards,

 

 

James Dexter :-)

Share this post


Link to post
Share on other sites
Guest Dave

James,

 

If you have a spare 8 bit port, feed you data lines into one 8 bit port one way round, and the other port the other way round.

 

Then read or write to the port which one is the right way round on that occassion.

 

Regards

Dave

Share this post


Link to post
Share on other sites

Hi Dave,

 

*GOOD* idea!!  

 

Really I hadn't actually thought of that but I can then set my ports up in advance and it makes the whole thing simpler...

 

Thanks for that.... all I need to do now is read fsk data @ 1200 baud data and I'm away ;-)

 

 

Best regards,

 

 

James Dexter :-)

Share this post


Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...