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

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

Children
No data