How to do bitwise shift across several variables/array members?

# Thread: How to do bitwise shift across several variables/array members?

1. ## How to do bitwise shift across several variables/array members?

Say there is an array, having 16 member, 1 byte each. I want to shift bits say to right in that way, that the byte that "leaves" say 1st member, gets shifted in into 2nd member, and so on (and bit shifted out from last member, shifts into 1st member). Are there any simple ways to do this, except that reading each member bit by bit, assembling in new variable and writing back?
Last edited by CuriousOne; - 14th October 2021 at 09:40.

3. ## Re: How to do bitwise shift across several variables/array members?

So you want to say, I can shift left or right whole contents of an array?

4. ## Re: How to do bitwise shift across several variables/array members?

Are you using the shiftout command? If so you can select MSB or LSB first.

5. ## Re: How to do bitwise shift across several variables/array members?

No, there is no "one command way" to shift or rotate bits spanning multiple members of an an array. You will have to write some code to do that.

6. ## Re: How to do bitwise shift across several variables/array members?

Start with the least significant byte, use the Shift-Left "<<", then check the STATUS Register Carry bit to see if you have to add one to the more significant byte after shifting.

7. ## Re: How to do bitwise shift across several variables/array members?

I'm thinking it in the following way (as example, 2 byte array, shifted to right)

1. Read both array bytes into variables A and B
2. Shift B right 1 bit
3. Add LSB from A as MSB to B
4. Shift A right
5. Write A and B back to array

Repeat as desired.

Correct?

8. ## Re: How to do bitwise shift across several variables/array members?

Yes, assuming you really do want a shift and don't want to rotate the value shifted out of B back into the MSB of the array.
There's no need to copy the array bytes to A and B, but if it helps you keep it straight then go for it.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts