What about the conditions...
This is the function i wrote, but what should i do in case x receives a negative value? Should i rather declare setbits as int and then check if x is negative? Seems like the program automaticaly turns it into a positive value if its negative, and thats not good...
Code:
unsigned setbits(unsigned int x, int p, int n, unsigned int y)
{
if(n < 1 || n>p-1
|| p < 0 || p>sizeof(unsigned)*8)
return 0;
unsigned tmp1 = x&(~((~(~0<<n))<<p+1-n));
unsigned tmp2 = ((y&(~(~0<<n))))<<p+1-n;
return tmp1 | tmp2;
}