PDA

View Full Version : How to do bitwise shift across several variables/array members?



CuriousOne
- 14th October 2021, 08:17
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?

gebillpap
- 14th October 2021, 14:52
here you will find what you need http://www.picbasic.co.uk/forum/showthread.php?t=544&highlight=Bits%2C+Bytes+Words+and+Arrays

CuriousOne
- 14th October 2021, 17:36
So you want to say, I can shift left or right whole contents of an array?

peterdeco1
- 14th October 2021, 18:11
Are you using the shiftout command? If so you can select MSB or LSB first.

HenrikOlsson
- 14th October 2021, 18:36
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.

mpgmike
- 14th October 2021, 18:38
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.

CuriousOne
- 14th October 2021, 18:46
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?

tumbleweed
- 14th October 2021, 23:19
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.