# Thread: how can I shift bit right a byte array

1. ## 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. 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. 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. Hi everyone, I really need your help. How can I shift byte array as above. Thank you so much.

5. Originally Posted by lovesunset21
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. 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. 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. 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. 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. 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. 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