Possible Duplicate:
Explanation of an algorithm to set, clear and test a single bit
I have an unsigned char
. I would like to have bits 2 through 4 (counting from the least significant bit as 0) copied to another unsigned char
as the first three bits. For example, in
abcdefgh // a,b,c,d,e,f,g,h are 0 or 1
becomes
00000def
I have tried
unsigned char input, output;
output = (input << 3) >> 5;
which does not work, but
output = (input << 3)
output >>= 5;
does work.
Is there a way in C to accomplish this in one line?