Thread: How to shift the bits of an array?

  1. #1
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657

    How to shift the bits of an array?

    I was playing around with an earlier program and found that it (if possible in a simplistic way) could lead to an easy solution.

    Is it possible to shift the bits of a character array (without resorting to casting pieces of it to integers, iterating and keeping track of 'would be lost' bits) ?
    If so, how ?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You need to give an example of what you are trying to do. Shift how? Wrap the left most bit around to the right?
    Code:
    char foo[ 2 ];
    foo[0], bit 0 = foo[ 1 ], bit 7 ?

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Could be, but not necessary.
    I thought of it like this:
    If I (right)shift the contents of
    foo[5]="mouse";
    by 8*n or 8*n*sizeof(char):
    It should modify(when n==1) foo[] to become:
    "#mous"
    If # can be 'e' , then nice, if not ..doesn't matter.

    What I'm looking for is a easy way to do so...possibly with (somehow!) applying the >> directly to foo .
    Last edited by manasij7479; 08-13-2011 at 06:10 PM.

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    AFAIK, you are going to be limited by your register size. So, assuming 32 bit register, you will not be able to bit shift more than 4 characters (assuming 8 bit characters for the standard platform) at any one time. Additionally, right and left shifting does not retain the values of those bits shifted off the end. They are gone into the ether never to be seen again. (of course unless you save the values into a second buffer)In order to keep them you could dig down into ASM and look at the ROR and ROL commands vice right and left bit shifting.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  5. #5
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by AndrewHunter View Post
    AFAIK, you are going to be limited by your register size. So, assuming 32 bit register, you will not be able to bit shift more than 4 characters (assuming 8 bit characters for the standard platform) at any one time. Additionally, right and left shifting does not retain the values of those bits shifted off the end. They are gone into the ether never to be seen again. (of course unless you save the values into a second buffer)In order to keep them you could dig down into ASM and look at the ROR and ROL commands vice right and left bit shifting.
    But they can contain the address of the array in memory.
    Can't the array be modified from there?
    Also, the bits disappearing over the edge does not bother me, only they must not disappear from the middle. It seems that there is no option other than running a loop(in which it would be easier to deal with the characters themselves at a higher level).
    Last edited by manasij7479; 08-13-2011 at 07:18 PM.

  6. #6
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    You are correct, they can be rotated in memory, my bad. As for "them not falling off the middle" the middle changes everytime you shift so I am not sure what you mean. Either way, you loose information with bit shifting.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  7. #7
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    O.. By middle, I mean the edges of the pieces the processor can shift at once...as you suggested 32 bit integers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Shift all '1' bits in a number to the left end
    By beatbox32 in forum C Programming
    Replies: 10
    Last Post: 07-06-2011, 01:38 PM
  2. Shift right for an array .
    By amir1986 in forum C Programming
    Replies: 4
    Last Post: 01-22-2011, 06:56 AM
  3. C how to Shift bits in BCD
    By evariste in forum C Programming
    Replies: 13
    Last Post: 12-17-2010, 03:50 AM
  4. How to shift elements of an array?
    By zeebo17 in forum C Programming
    Replies: 25
    Last Post: 06-09-2010, 07:44 PM
  5. array shift problem
    By splendid bob in forum C++ Programming
    Replies: 3
    Last Post: 07-26-2002, 10:11 PM