Thread: Help! I am going crazy. Bit shifting a byte array

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    18

    Help! I am going crazy. Bit shifting a byte array

    Hello

    I really hope someone can help me with this. I have been working on it all weekend and I'm getting nowhere.

    I need to left shift (with a rotate) an unsigned char array.

    The array looks like this:

    11110000 11001100 10101010 11110000

    I have tried left shifting using a pointer to the array like so:

    *array_ptr <<= 1;

    But that does nothing.

    Does anyone know of an easy way I can left shift the entire contents of the array? Also, if it could include a rotate (msb becomes lsb) that would be fantastic.

    Thank you.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Shift the bits of a byte one byte at a time.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    18
    Thanks for your reply.

    If I do that don't I need to then keep track of every bit, for example -

    Move the first bit of the second byte to the first byte, making it the lsb.
    Move the first bit of the third byte to the second byte, making it the lsb.
    Move the first bit of the fourth byte to the third byte, making it the lsb.

    What if I need to do two left shifts? (as in, two left shift operations in one go.)

    Surely there is a way to operate on the array all at once?

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    I'll take you at your word about trying for some time, but it is generally advisable to post more of the code that show it. That said, I like messing with bits, so this is what I was playing with:
    Code:
    #include <stdio.h>
    #include <limits.h>
    
    void show(const void *object, size_t size)
    {
       const unsigned char *byte;
       for ( byte = object; size--; ++byte )
       {
          unsigned char mask;
          for ( mask = 1 << (CHAR_BIT - 1); mask; mask >>= 1 )
          {
             putchar(mask & *byte ? '1' : '0');
          }
          putchar(' ');
       }
       putchar('\n');
    }
    
    void shiftl(void *object, size_t size)
    {
       unsigned char *byte;
       for ( byte = object; size--; ++byte )
       {
          unsigned char bit = 0;
          if ( size )
          {
             bit = byte[1] & (1 << (CHAR_BIT - 1)) ? 1 : 0;
          }
          *byte <<= 1;
          *byte  |= bit;
       }
    }
    
    int main()
    {
                               /* 11110000 11001100 10101010 11110000 */
       unsigned char array[] = {      0xF0,    0xCC,    0xAA,    0xF0};
       show(array, sizeof array);
       shiftl(array, sizeof array);
       show(array, sizeof array);
       return 0;
    }
    
    /* my output
    11110000 11001100 10101010 11110000 
    11100001 10011001 01010101 11100000 
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Mar 2008
    Posts
    18
    Thank you, that works, I will examine your code to make sure I understand it.

    Thanks again

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
       for ( byte = object; size--; ++byte ) /* Go byte by byte. */
       {
          unsigned char bit = 0;
          if ( size )
          {
             /* Grab the high bit of the next byte. */
             bit = byte[1] & (1 << (CHAR_BIT - 1)) ? 1 : 0;
          }
          *byte <<= 1;   /* Shift the current byte. */
          *byte  |= bit; /* Bring in the high bit of the next byte. */
       }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Registered User
    Join Date
    Dec 2008
    Posts
    1
    The following values are stored into my array of bits:

    00010000 11000010 01011111 00000001

    6543210 65432107 65432107 7 <- expected bit order

    I need to shift the above bits in order to accomplish the following:

    01000010 00001000 11100001 10101111


    Both array of bits were filled using the following macro:

    #define SET_BIT(array,index) (array[index>>3] |= (1<<(index & 0x07)))

    The problem is that I do not yet realize how to do it and do not understand your what ? 1 : 0;
    means in your code.

    Any help will be much appreciated!

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    You're not supposed to revive threads that have had no activity for months, particularly when you're not the original poster. Please start a new thread, and if it helps you, you may include a link to this one in your post.

    Probably a good idea to read over all the rules too. It wont take you too long, but you need to spend some time to read them if you want others to spend the time to help.
    Cheers, iMalc.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. convert 32 bit number to array of digits
    By droseman in forum C Programming
    Replies: 11
    Last Post: 02-18-2009, 09:37 AM
  2. Hex String to byte array conversion
    By IfYouSaySo in forum C# Programming
    Replies: 3
    Last Post: 06-15-2006, 04:59 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM