PIC Microcontoller Bit Math Method

Rotate some bits without affecting others

What if you need to shift a 5 bit number in, through and out of a byte using bit0 to bit4 and not affect bit5 - bit7 because, for example, bits 5-7 are running the column select (via a 3 to 8) for a matix LED and the lower 5 bits are connected to the colum.

If you had two 8 bit wide registers next to each other like the following. The 5 bit value in byte 2 is shifted left throughout byte 1 and out.

byte1     byte2

XXX00000  xxx11111      start state
XXX00001  xxx11110
XXX00011  xxx11100
XXX00111  xxx11000
XXX01111  xxx10000
XXX11111  xxx00000
XXX11110  xxx00000
XXX11100  xxx00000
XXX11000  xxx00000
XXX10000  xxx00000
XXX00000  xxx00000      end state

X = can not be changed.
x = not used

Dmitry Kiryashov [zews at AHA.RU] says:

1. You only need to shift single one register.

        rlf     byte1,W         ;get new bit from carry

        xorwf   byte1,W         ;get difference
        andlw   B'00011111'     ;0's will mask unchanged bits
        xorwf   byte1,F         ;update only required bits

2. You need to shift in such way through many registers.

        rlf     byte1,W         ;get new bit from carry
        andlw   B'00111111'     ;mask msb's but last
        addlw   B'11100000'     ;copy last to carry

        xorwf   byte1,W
        andlw   B'00011111'     ;see explanation of
        xorwf   byte1,F         ;this in example above
;then
;
        rlf     byte2,W
        andlw   B'00111111'
        addlw   B'11100000'
;
;and so on as many bytes as you need ;)


Interested: