• C

# 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
###### Who is Participating?

Commented:
You mean something like having an array like so:

char reverse_lut[256];

With the values filled in so that reverse_lut[N] is N with its bits in reverse order ?

Anyway, an easy way to reverse the bits in a byte, without a LUT, is in three steps, as follows:

First step:  swap bits 0123 with bits 4567  { byte = ((byte & 0x0F) << 4) | ((byte & 0xF0) >> 4); }
Second step: swap bits 01 with 23 and 45 with 67 simultaneously   { byte = ((byte & 0x33) << 2) | ((byte & 0xCC) >> 2)); }
Last step: swap bits 0 and 1, 2 and 3, 4 and 4, 6 and 7 simult.   { byte = ((byte & 0x55) << 1) | ((byte & 0xAA) >> 1)); }
NB: You can do those steps in any order you like, you can do them on 4 bytes simultaneously without speed loss (just change the ANDing values), and if you want to do some MMX assembly, should be doable on 8 bytes simultaneously as well.
0

Data Warehouse Architect / DBACommented:

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
0

Author Commented:
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
0

Data Warehouse Architect / DBACommented:

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
0

Commented:
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.
0

Data Warehouse Architect / DBACommented:

Hi sneeuw_chan,

The performance of an LUT can be tricky to predict.  It does, after all, require memory fetches and the speed of the fetch is contingent on where in memory/cache the table lies.  The first one might be pretty expensive, but subsequent fetches should be very fast as the table will have been moved to the closest cache.

Looping is effective for a single byte, but only because these processors are so fast when not referencing memory.  I certainly wouldn't build a length swap algorithm on it though.

The algorithm that you suggested is what I refered to earlier.  It's a little tough for newcomers to understand, but lightening fast.  The instruction count (and ultimately execution speed) increases incrementally while the data length increases exponentially.  (3 steps to swap 8 bits, 4 steps for 16, 5 steps fo 32.)  Past the register size (32 or 64 bits) the algorithm timing synchronizes with the size increase of the data.

Swapping bits on large data block is kinda fun, too.  :)

Kent
0

Commented:
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.
0

Data Warehouse Architect / DBACommented:

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

0

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

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

{
x=lut[x];
write x,z;
}

Regards
Aakash

0

Commented:
Hi Arut,

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

-ssnkumar
0

Data Warehouse Architect / DBACommented:

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
0

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

-ssnkumar
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.