Before I prompt the user to input their own values of x, p, n and y I am sending set values I know the desired output for.
Code:
#include <stdio.h>
unsigned int SetBits(unsigned int x, int p, int n, unsigned int y);
void DisplayBits(unsigned value);
int main()
{
unsigned int k;
k = SetBits(170, 5, 3, 167);
DisplayBits(k);
printf("%d\n", k);
return 0;
}
unsigned int SetBits(unsigned int x, int p, int n, unsigned int y)
{
return ((x & ~(~(~0 << n) << p + 1 - n)) | ((~(~0 << n) & y) << p + 1 - n));
}