Link to home
Start Free TrialLog in
Avatar of arut
arut

asked on

Reversing bytes

Hi ALL,

I would like to know how a byte coming into a port of a device
can be quickly reversed and sent on another port quickly using
a lookup mechanism (array).

Assumption:  Memory is abundant

Thanks,
Arut
Avatar of Kent Olsen
Kent Olsen
Flag of United States of America image


Hi Arut,

I'm not sure what you're asking here.  Is the port management giving you trouble or the byte reversal?  (And what do you mean by byte reversal?  Send the bytes in reverse order of the way that the arrive or reverse the bits within the byte?)


Kent
Avatar of arut
arut

ASKER

Hi Kent,

Sorry about that. I meant bits to be reversed LSB becomes MSB bit. This is a question someone asked me and I had no idea.

Thanks,
Arut

There are quite a few ways to reverse bits in a byte.  The most "obvious" way is just brite force:

int ReverseBits (int Source)
{
  int idx;
  int Result;

  Result = 0;
  for (idx = 0; idx < 8; idx++)
    if (Source & (1 << idx))
      Result |= (1 << (7 - idx));
  return (Result);
}

You can also reverse the bits with some clever math, but it's a lot tougher to understand.

Good Luck,
Kent
ASKER CERTIFIED SOLUTION
Avatar of sneeuw_chan
sneeuw_chan

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Additional note: The MMX bit is assuming you're on intel, you can probably do it similarly on other architectures with as many bytes as you can fit into a machine register.

In any case, the code I proposed should be faster than a LUT, assuming you're able to work on multiple bytes.  A LUT is probably faster than doing it on single bytes or two bytes at a time, and could be comparable to four at a time, dunno.  You'd have to test that.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yeah, I know, caching is a pain, performance wise.  But I don't want to assume the OP is working on a PC-type processor, who knows, maybe he's on an embedded system with an ARM or whatever.  Easiest is to write it both ways and to compare performance in typical usage, and add a big fudge factor.

If you really want optimal performance, you should probably also take care of parralelizing instructions, modern CPUs don't like it if the data from one instruction is needed by the very next instruction.  Doing two machine-words at once should take care of that adequately, although you may run out of registers then, dunno.  Long time since I spent hand-tuning MMX instructions.  Was fun though.. ^_^

But I digress, the OP probably got his answer already, and if not, he'll come up with more pointed questions, one would assume.

A lot of these threads get more interesting once the original question is answered.  :)

If i have understood the question right , he just wants to reverse a byte.. and byte according to its definition is just 8bits and shud be platform independent...

so have an array like

int a[256]={
0,    //reverse of 0 is still 0
128, //since reverse of 1 is 128 in bytes
64,//reverse of 2 is 64
..
...
.
.
255//Reverse of 255 is 255 only
};
and so on

and when u receive a byte x from port y and have to write it to port z ur algo is only

read x,y  //read x from port y
x=lut[x]; // reversing
write x,z //write x to port z

It saves u a lot of time.. Moreover if u are lazy to find out all the values and hard code them into the c file..
Write a function which calculates them and stores them into the table when the program first starts...

Eg..

calc_lut(); //reverses and stores the values in lut

while(read x,y)
{
x=lut[x];
write x,z;
}


Regards
Aakash

PS> Hope this answers your Q
Hi Arut,

   Can you explain by giving a small example, so that we can suggest better solutions!?

-ssnkumar

Hi aakash,

I agree with you -- his question suggests that he wants to reverse the bits in a byte.  But the Post Script (memory is abundant) suggests that he's got more in mind than just reversing the bits in a byte.


Kent
Does he wants to swap 1st and 8th bit, or 1st and 4th bit?

-ssnkumar