s wrote:
Here's a better explanation of what I'm really trying to do:
unsigned char target = 0xBD;
unsigned char small_value = 2;
unsigned position = 0xC0;
I need to put the two bits in small_value into target at the place
indicated by the set bits in position.
Here's one way to do it. It is not a very elegant solution, nor is the
output particularly interesting.
#include <stdio.h>
int main(void)
{
unsigned char target = 0xBD;
unsigned char small_value = 2;
unsigned char position = 0xC0; /* must be non-zero */
unsigned char temp = position;
int i = 0;
while (!(temp & 1))
{
temp >>= 1;
i++;
}
target &= ~position;
target |= (small_value << i);
printf("%#2X\n", target);
return 0;
}
--
Tim Hagan