excuse me if this is a newbie question:
I have four uint8_t variables: first, second, third, fourth.
My objective is to put them in a uint32_t, such as that the uint32_t will be composed as:
fourth-third-second-first, but only putting the first 7 less significant bits of every uint8_t into the uint32_t, and so padding the 4 most significant bits of the uint32_t with zeros.
For example, let's say:
first = 10000000 -> I'm gonna put 0000000
second = 10011001 -> I'm gonna put 0011001
third = 10101010 -> I'm gonna put 0101010
fourth = 01111111 -> I'm gonna put 1111111
The uint32_t should end up being:
00001111 11101010 10001100 10000000
That is: 4zerosOfPadding-fourth-third-second-first
How can I do this using masking and shifting?
Edit: What I tried is:
uint32_t target = 0;
uint8_t first = 128, second = 153, third = 170, fourth = 127;
//127 = 0111 1111
target = (first & 127);
target = (target >> 7) | (second & 127);
target = (target >> 14) | (third & 127);
target = (target >> 21) | (fourth & 127);
But what I get with this is that I just overwrite target everytime with the 7 less significant bits of the current uint8_t. I can't understand how to use shifting properly. Thanks everyone for the help.