Let's try 2-6, to give you a taste of how bit operations work, and then see if it makes sense.
int setbits( int x, int p, int n, int y )
{
int bitmask = ~0, y_right = 0;
/* Ok, first let's get y's rightmost bits.
* Take our bitmask, shift it left by n bits, leaving the least significant
* n bits as 0. Then, flip the bitmask so the rightmost n bits become 1, and the
* leftmost n bits become 0.
*
* Then, AND this bitmask with y, to yield y's n rightmost bits.
*/
bitmask = ~( bitmask << n );
y_right = bitmask & y;
/*
* Ok, now let's use y_right as our bitmask for x.
* Shift y_right to the left to *begin* at position p.
*/
y_right <<= p - n;
x |= y_right;
return x;
}
Note: the above may be completely incorrect as I haven't tested it, but it should give you a decent idea to start with.