Encoders
Hub for Computer Whizzes Register to join us

Encoders > More... > C > Program

/* Function that returns x with the n bits at position p inverted. */

#include <stdio.h>

unsigned invert(int x, int p, int n);

main()
{
        printf("%d\n",invert(107,4,3));
}

unsigned invert(int x, int p, int n)
{
        return x & ~(~(~0<<n) << p-n+1) | ~(x | ~(~(~0<<n) << p-n+1));
}