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.

  • So what you want is a sort of 'shift with carry.' Looks easy enough:

    array[24] >>= 1;
    array[24] |= (array[23] << 7);
    array[23] >>= 1;
    array[23] |= (array[22] << 7);
    


    and so on. Feel free to make a for loop out of it.
    If you care about speed or code size, you should implement this in assembly using the RRC instruction.

    Regards,
    - mike

  • " want to shift the all array right/left by 1 bit."

    Do you mean you want to shift each element independently?

    Or do you want to shift bits out of one element and into its adjacent element?

  • #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
    

  • i want to shift the bits out of one element and into the start element like a loop.
    in this case i want to rotate all the bits in the array so no bit will lost. the last bit(msb) will enter the place of the first bit(lsb).

  • many thank for Per Westermark
    very elegant, do exactly what i mean.

  • i added this to Per Westermark code so after 192 loops all the array is rotating to its original form.

    unsigned char array_1[25] = {
    0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x20,0x21,0x22,0x23,0x24,0x00};
    
    for (i = 0; i < sizeof(array_1); i++) {
      printf("%02bx ",array_1[i]);
      }
    printf("\n");
    
    for (j = 0; j < 192; j++) {
      // Shift all data right one bit,
      // with carry from lower index.
      if (array_1[23] & 0x01) {
             array_1[24] = 0x80;  // buffer for exit '1'
             }
         for (i = 23; i >= 1; --i) {
           array_1[i] =
              (array_1[i]>>1) | ((array_1[i-1]<<7) & 0x80);
             }
      //for (i = 0; i < sizeof(array_1); i++) {
             printf("%02bx ",array_1[i]);
             }
      // possibly take care of rotate from last entry too.
      array_1[0] >>= 1;
      // insert the '1' to entery point
      array_1[0] |= array_1[24];
      for (i = 0; i < sizeof(array_1); i++) {
           printf("%02bx ",array_1[i]); }
      printf("\n");
      array_1[24] = 0x00;  // reset buffer
    }
    

    thanks again