# K & R C Programs Exercise 2-6.

K and R C, Solution to Exercise 2-6:
K and R C Programs Exercises provides the solution to all the exercises in the C Programming Language (2nd Edition) You can learn and solve K&R C Programs Exercise.
C Function setbits(x, p, n, y) that returns x with the n bits that begin at position p set to the rightmost n bits of y, leaving the other bits unchanged.Read more about C Programming Language .

`/************************************************************ You can use all the programs on  www.c-program-example.com* for personal and learning purposes. For permissions to use the* programs for commercial purposes,* contact [email protected]* To find more C programs, do visit www.c-program-example.com* and browse!* *                      Happy Coding***********************************************************/#include <stdio.h>unsigned setbits(unsigned x, int p, int n, unsigned y){ return (x & ((~0 << (p + 1)) | (~(~0 << (p + 1 - n))))) | ((y & ~(~0 << n)) << (p + 1 - n));}int main(void){ unsigned i; unsigned j; unsigned k; int p; int n; for(i = 0; i < 30000; i += 511) {  for(j = 0; j < 1000; j += 37)  {   for(p = 0; p < 16; p++)   {    for(n = 1; n <= p + 1; n++)    {     k = setbits(i, p, n, j);     printf("setbits(%u, %d, %d, %u) = %un", i, p, n, j, k);    }   }  } } return 0;}`
`Read more Similar C ProgramsC BasicK and R C Programs Exercise`

You can easily select the code by double clicking on the code area above.