Monday, December 3, 2012

Write a 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.



                Write a 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.

So there is x = 32 (0010000)
Y=19 (0010011)

Now p =4
and n =2

So right most 2 bits of y are 11.
So we move these two bits to X position (5,6).

After this code X=48(0110000)


#include
#include


void countNumberOfbits();
int main() {
countNumberOfbits();
return 0;
}

void countNumberOfbits() {
char x = 32;
char y = 19;
int p =4;
int n = 2;

y = y & (~0 << 4);
y = y & (~0 >> 2);
x = x|y;



printf("\n Count is ---:%d: --\n",x);fflush(stdout);
}


No comments: