how can I shift bit right a byte array

This is a discussion on how can I shift bit right a byte array within the C Programming forums, part of the General Programming Boards category; Hi everyone, I need your help in shifting right the byte array. For example: I have 3 bytes : s1s2s3s4s5s6s7s8 ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    37

    how can I shift bit right a byte array

    Hi everyone, I need your help in shifting right the byte array. For example:

    I have 3 bytes :

    s1s2s3s4s5s6s7s8 s9s10s11s12s13s14s15s16 s17s18s19s20s21s22s23s24

    ---> 0s1s2s3s4s5s6s7 s8s9s10s11s12s13s14s15s s16s17s18s19s20s21s22s23

    I am looking forward to your answer thank you so much.

  2. #2
    Registered User
    Join Date
    Sep 2008
    Posts
    200
    Please learn that Google is your friend. As it happens, the fourth link for a search of "c bit shifting" points to a tutorial on this very site.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    37
    I don't know how to shift bit in a byte array with the first bit of a byte is the last bit of the previous as above. I have searched in Google.

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    37
    Hi everyone, I really need your help. How can I shift byte array as above. Thank you so much.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by lovesunset21 View Post
    Hi everyone, I really need your help. How can I shift byte array as above. Thank you so much.
    Did you read the tutorial John pointed you to?

    The general rule here is that we're not going to write code for you but we will help you sort out problems in code you have written. I'd say give it your best shot, post your code and go from there...

  6. #6
    Registered User
    Join Date
    Nov 2008
    Posts
    120
    I don't think he wants to do any bitwise operations. Sounds like he wants to shift the elements in an array over.

    Let me google that for you

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    If your most significant bit is to the right in your illustration? And you only have a three element char array (unsigned i presume) you can cast it to an unsigned int and do the shift on all of them.

    Code:
    int main()
    {
            unsigned char arr[] = {1,1,1};
    
            unsigned int *quad = (unsigned int*)arr;
            *quad <<= 1;
    
    
            return 0;
    }
    If you have a larger array it's going to be more complicated, you need to query the most significant bit and keep it in a temp variable before you shift.

  8. #8
    Registered User
    Join Date
    Oct 2010
    Posts
    37
    Thanks for your help. This is my code. I have an unsigned char array with 36 elements.
    Code:
    unsigned char s[36]
    
    
    #define bit_at(a,i) !!(a[i / 8] >> (8 - (i % 8) - 1) & 1)  //I copy this code from our forum.
    #define bit_clear(a,i) ((a) & ~(1 << (i)))
    #define bit_set(a,i) ((a) | (1 << (i)))
    
    void shiftbit(unsigned char *s, unsigned char t1, unsigned char t2, unsigned char t3){
    int i;
    unsigned char a=0;
    unsigned char b=bit_at(s,7); //b is the 8th of previous byte
    
    for (i=0;i<36;i++){
        
       //a is the 8th bit
        a=bit_at(s,(i+1)*8-1);
        
       //shift right each byte
        s[i]=s[i]>>1;
        
        
        if (i!=0) 
          //if b=0, set the first bit of the byte to 0.
           if (b==0) bit_clear(s[i],7);
           else bit_set(s[i],7);
        b=a;      
    }


    My array s is:


    11111010 10100111 01010100 00000001 10101110 01011011 00001000 10110101 01100010 00001111 00000000 00000110 00111011 00000111 11001100 10010001 01011110 00100010 11101111 10110100 01111001 01000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000111


    After I shift, it is:


    00000000 01010011 00101010 00000000 01010111 00101101 00000100 01011010 00110001 00000111 00000000 00000011 00011101 00000011 01100110 01001000 00101111 00010001 01110111 01011010 00111100 00100000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000011

    That's my problem. Please help me. Thank you so much.

  9. #9
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    By shifting to the right you make the values smaller, which means that the least significant bit will be "pushed over the edge". You need to store the LSB, which to my way of thinking is the 1st bit. I'm not going to decode those macros, but it seems like the bit is lost between each char in your output.

    You could check if the LSB is set by doing:

    Code:
    bool is_set = a[i] & 1;
    Then, if it is, set it again at the next index of a, after you have shifted:

    Code:
    a[i+1] >>= 1;
    if(is_set)
            a[i+1] |= 0x80;
    Now, by shifting here you once again need to store the LSB, so you probably need a second variable.

  10. #10
    Registered User
    Join Date
    Oct 2010
    Posts
    37
    Thank you so much. I try and It works right now. It's great.

    I have one more question. If I want to set bit at a position into a bit t. What should I do?

    For example:

    I have bit t.

    In the s array of 36 elements as above:

    I want to change the 94th bit into t.

    I have tried but it seems impossible. Thank you so much.

    my code is
    Code:
    a[10]&=t<<2;

  11. #11
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    Are you asking how to clear a bit? If so,

    a[10] &= ~(1 << 2);

    To set it

    a[10] |= (1 << 2);

    This sets or clears bit 2 in a[10]. Your code would check the value of t. If t is 0, clear the bit. If t is 1, set the bit.

    Or alternatively, always clear the bit out. Then OR in t using a[0] |= (t << 2).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 15
    Last Post: 08-30-2010, 10:26 AM
  2. Converting an array of byte to an array int
    By chrisfarrugia in forum C Programming
    Replies: 7
    Last Post: 03-05-2008, 08:30 AM
  3. Replies: 16
    Last Post: 11-23-2007, 01:48 PM
  4. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 06:49 PM
  5. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 04:42 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21