PDA

View Full Version : Shifting a whole array sideways by one bit ( >>1)



mr.sneezy
- 19th February 2013, 07:49
I'm after a clue as to the least messy way to shift an array of 11 bytes by one bit right. Basically I have a situation where my program reads 88 bits of data from a pin into an array via a pulsin command. Every once in a while the first pulse of the 88 bits is missed, leaving 87 bits of good data offset by 1 bit. I know the missing (first) bit is always a logic 1, so I can add that back to the first read byte easily enough. The mess starts when I think of a way to ripple the one bit shift through the remaining 10 bytes, while transferring the high bit from one byte to the low bit of the next after the shift.

Has anyone attempted this before and found a neater way of doing it ?

So far I have this idea by using a shift on a two bytes at a time and keeping one of the bytes

MyArray var byte[11]
MyWord var word

'load two sequential bytes off the end of the array into a word
MyWord.lowbyte = MyArray[10] '10 is the 11th byte of the array
MyWord.highbyte = MyArray[9]

'shift the whole word by 1 bit
MyWord = MyWord >> 1

'copy the new last byte back to the array
MyArray[10] = MyWord.lowbyte

'then pull out byte 9 & 10 and repeat, then 8 & 9, etc, etc all the way back to bytes 0 & 1

Messy. Anyone got another way, maybe manipulating the array more directly somehow ?

Martin

HenrikOlsson
- 19th February 2013, 08:14
How about

myArray VAR BYTE[11]
Index VAR BYTE

For Index = 0 to 87
myArray.0[Index] = myArray.0[Index+1]
NEXT

/Henrik.

Ioannis
- 19th February 2013, 09:28
If bits are 88 then I think we need 86 iterations in the loop and fill the last bit in the myArray by a 0.

Ioannis