Thread: printing array's content with offset different behavior

  1. #16
    Registered User
    Join Date
    Jan 2016
    Posts
    84
    You're right, sorry for the mess.

    Here's the code after modifications, yes e is from previous code I was working on and forgot it

    But also, after removing e, the same problem still not solved.

    Code:
    uint8_t i,k,e;uint8_t buffer1[40],bit_mask[40];                  // buffer1 for shifting data & bit_mask for saving the carried bit
    
    
    for (k=0;k<40;k++){                                // initialize both arrays to 0
    buffer1[k]=0;bit_mask[k]=0;}
    
    
    for (k=0;k<8;k++)
    buffer1[k]=0x80;                                   // put this value for testing in the first 8-bytes then shift it to the next 8-bytes
    
    
    for (i=0;i<40;i++)
    {
        printf("shift %d\n",i);                        // prints no. of shifts
    
    
        for (k=0;k<40;k++)
        {
            printf("0x%.2x\t",buffer1[k]);       // print the data in order of 8-bytes at total of 40-bytes
           if (k%8==7)printf("\n");                    // after printing 8-bytes put a new line
        }
        printf("\n");                                  // new line after 40-bytes of each  shift
    
    
        for (k=0;k<40;k++)                             // scan whole array
        {
            if (buffer1[k] & 0x80)                     // test MSB of each byte
            bit_mask[k+8] = 0x01;                      // taking the address of the next parallel byte
        }
    
    
        for (k=0;k<40;k++)                             // shift every byte
        buffer1[k]<<=1;
        for (k=0;k<40;k++)                             // copy the carried bit to the parallel byte
        buffer1[k]|=bit_mask[k];
    
    
        for (k=0;k<40;k++)                             // clear bit_mask content for the next test
        bit_mask[k]=0;
    }

  2. #17
    Registered User
    Join Date
    Jan 2016
    Posts
    84
    OK, problem solved by including this if statement:

    Code:
        for (k=0;k<40;k++)                             // scan whole array    {
            if (buffer1[k] & 0x80)                     // test MSB of each byte
            {
                if ((k+8)>40)continue;
            bit_mask[k+8] = 0x01;
            }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-25-2015, 01:26 AM
  2. How To: Content of Array to String
    By rishiraj in forum C Programming
    Replies: 7
    Last Post: 04-17-2014, 12:57 PM
  3. How to copy the content of an array into a pointer
    By firstoption in forum C Programming
    Replies: 2
    Last Post: 09-22-2011, 09:31 AM
  4. printing array content - spot the error
    By nosnowking in forum C Programming
    Replies: 5
    Last Post: 02-04-2009, 05:50 PM
  5. changing the array content !!
    By mehuldoshi in forum C Programming
    Replies: 2
    Last Post: 06-29-2002, 06:33 AM

Tags for this Thread