# Thread: How to shift the bits of an array?

1. ## How to shift the bits of an array?

I was playing around with an earlier program and found that it (if possible in a simplistic way) could lead to an easy solution.

Is it possible to shift the bits of a character array (without resorting to casting pieces of it to integers, iterating and keeping track of 'would be lost' bits) ?
If so, how ?

2. You need to give an example of what you are trying to do. Shift how? Wrap the left most bit around to the right?
Code:
```char foo[ 2 ];
foo[0], bit 0 = foo[ 1 ], bit 7 ?```

Quzah.

3. Could be, but not necessary.
I thought of it like this:
If I (right)shift the contents of
foo[5]="mouse";
by 8*n or 8*n*sizeof(char):
It should modify(when n==1) foo[] to become:
"#mous"
If # can be 'e' , then nice, if not ..doesn't matter.

What I'm looking for is a easy way to do so...possibly with (somehow!) applying the >> directly to foo .

4. AFAIK, you are going to be limited by your register size. So, assuming 32 bit register, you will not be able to bit shift more than 4 characters (assuming 8 bit characters for the standard platform) at any one time. Additionally, right and left shifting does not retain the values of those bits shifted off the end. They are gone into the ether never to be seen again. (of course unless you save the values into a second buffer)In order to keep them you could dig down into ASM and look at the ROR and ROL commands vice right and left bit shifting.

5. Originally Posted by AndrewHunter
AFAIK, you are going to be limited by your register size. So, assuming 32 bit register, you will not be able to bit shift more than 4 characters (assuming 8 bit characters for the standard platform) at any one time. Additionally, right and left shifting does not retain the values of those bits shifted off the end. They are gone into the ether never to be seen again. (of course unless you save the values into a second buffer)In order to keep them you could dig down into ASM and look at the ROR and ROL commands vice right and left bit shifting.
But they can contain the address of the array in memory.
Can't the array be modified from there?
Also, the bits disappearing over the edge does not bother me, only they must not disappear from the middle. It seems that there is no option other than running a loop(in which it would be easier to deal with the characters themselves at a higher level).

6. You are correct, they can be rotated in memory, my bad. As for "them not falling off the middle" the middle changes everytime you shift so I am not sure what you mean. Either way, you loose information with bit shifting.

7. O.. By middle, I mean the edges of the pieces the processor can shift at once...as you suggested 32 bit integers.

Popular pages Recent additions