"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 ( 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 on other sites

What is the reason that you want to do that?

Share on other sites

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 on other sites

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 on other sites

Now my turn

```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 on other sites

I like it

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 on other sites

Thank you very much indeed - I'm *really* new to this but I'll post what happens when I've tried it.

Many thanks again....

Best regards,

James Dexter :-)

Share on other sites

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.

Regards

Dave

Share on other sites

Thanks Dave for the info on rewiring the ports...I may be a real beginner but I guessed that ;-)

Best regards,

James Dexter :-)

Share on other sites

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.

Regards

Dave

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 on other sites

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 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 :-)

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.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

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

×

×
×
• Create New...