Ttelmah Guest
|
|
Posted: Tue Jan 10, 2006 6:12 am |
|
|
There is no built in function, so you have to do this yourself. There are a number of approaches, depending on what you actually want to do. Remember that C itself contains shift operations for the data types, so for (say), an int32 variable:
val=val<<8;
Will move the bytes left by 8 bits (giving an effective *256). The compiler is also smart enough to replace this with byte moves, rather than 8 bit shifts. If though you want to work with (say), an array of bytes, then you should consider using a pointer, and updating this, rather than moving the data itself. I you look at the ex_sisr code (which shows a buffer used to handle serial data), here the pointer only is moved, rather than relocating the whole block of data. This is obviously more efficient, than moving large amounts of data around. Remember also, that a 'union', would allow you to declare a variable containing some data form, and in the same space, a byte array. Moving the data, then becomes a simple matter of manipulating this array. So:
(for a rotation, rather than a shift)
Code: |
union rblock {
int8 b[4];
int32 val;
} value;
void rotate8(union rblock * v) {
int8 temp;
temp=v->b[3];
v->b[3]=v->b[2];
v->b[2]=v->b[1];
v->b[1]=v->b[0];
v->b[0]=temp;
}
//Called as
rotate8(&value);
|
Would perform a bytewise rotation on the int32 value 'value.val'.
So there are a lot of different approaches, depending on what you actually need to do. In general, if the amount of data involved is large, I'd definately consider moving a pointer, rather than the data itelf.
Best Wishes |
|