I am implementing a C function unsigned invert(unsigned x, int p, int n)
that is returning x with the n bits that begin at position p inverted, leaving the others unchanged.
#include <stdio.h>
unsigned invert(unsigned x, int p, int n);
int main()
{
printf("%u\n", invert(11111111, 5, 4));
printf("%u\n", invert(10, 2, 2));
return 0;
}
unsigned invert(unsigned x, int p, int n)
{
return x^(~(~0<<n)<<p+1-n);
}
This is what I got so far and if I'm tracing the function through it should be right, but I keep getting 11111163 for the first test and 12 for the second test.