This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

shifting arrays

hi,

i'm using uVision3.33 , uC-80c31x2.
my question is:
I'v initilize buffer array which is been loaded with 24 Byte each interval time.
now i want to shift the all array right/left by 1 bit.
maybe an example will clarify bettet:

unsigned char array_1[24];
unsigned char array_1[24] = {
0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x20,0x21,0x22,0x23,0x24};


the array's values in the example are just for example.
probably the operand '>>' must be use.
the result i wish for is:
(shifting all the array right only one time)

unsigned char array_1[24] = {
0x00,0x81,0x01,0x82,0x02,.....,0x92};


thanks in advance.

Parents
  • #include <stdio.h>
    
    int main() {
        unsigned char array_1[24] = {
            0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
            0x09,0x10,0x11,0x12,0x13,0x14,0x15,0x16,
            0x17,0x18,0x19,0x20,0x21,0x22,0x23,0x24};
        int i;
    
        for (i = 0; i < sizeof(array_1); i++) {
            printf("%02x ",array_1[i]);
        }
        printf("\n");
    
        // Shift all data right one bit, with carry from lower index.
        for (i = 23; i >= 1; --i) {
            array_1[i] = (array_1[i]>>1) | ((array_1[i-1] << 7) & 0x80);
        }
        array_1[0] >>= 1; // possibly take care of rotate from last entry too.
        for (i = 0; i < sizeof(array_1); i++) {
            printf("%02x ",array_1[i]);
        }
        printf("\n");
    
        // Shift all data left one bit, with carry from higher index.
        for (i = 0; i < 23; i++) {
            array_1[i] = (array_1[i] << 1) | ((array_1[i+1] >> 7) & 0x01);
        }
        array_1[23] <<= 1; // possibly take care of rotate from first entry too.
        for (i = 0; i < sizeof(array_1); i++) {
            printf("%02x ",array_1[i]);
        }
        printf("\n");
        return 0;
    }
    

    Gives result:

    01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
    00 81 01 82 02 83 03 84 04 88 08 89 09 8a 0a 8b 0b 8c 0c 90 10 91 11 92
    01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
    

Reply
  • #include <stdio.h>
    
    int main() {
        unsigned char array_1[24] = {
            0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
            0x09,0x10,0x11,0x12,0x13,0x14,0x15,0x16,
            0x17,0x18,0x19,0x20,0x21,0x22,0x23,0x24};
        int i;
    
        for (i = 0; i < sizeof(array_1); i++) {
            printf("%02x ",array_1[i]);
        }
        printf("\n");
    
        // Shift all data right one bit, with carry from lower index.
        for (i = 23; i >= 1; --i) {
            array_1[i] = (array_1[i]>>1) | ((array_1[i-1] << 7) & 0x80);
        }
        array_1[0] >>= 1; // possibly take care of rotate from last entry too.
        for (i = 0; i < sizeof(array_1); i++) {
            printf("%02x ",array_1[i]);
        }
        printf("\n");
    
        // Shift all data left one bit, with carry from higher index.
        for (i = 0; i < 23; i++) {
            array_1[i] = (array_1[i] << 1) | ((array_1[i+1] >> 7) & 0x01);
        }
        array_1[23] <<= 1; // possibly take care of rotate from first entry too.
        for (i = 0; i < sizeof(array_1); i++) {
            printf("%02x ",array_1[i]);
        }
        printf("\n");
        return 0;
    }
    

    Gives result:

    01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
    00 81 01 82 02 83 03 84 04 88 08 89 09 8a 0a 8b 0b 8c 0c 90 10 91 11 92
    01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
    

Children
No data