returns x with the n bits that begin at position p inverted(i.e, 1 changed into 0and vice versa), leaving the others unchanged. if n is greater than p, then show"falure".
输入:x,n,p
输出:如上所述
例 输入:13
4
2
输出:17
#include <stdio.h>
#include <assert.h>
unsigned invert( unsigned x, int p, int n )
{
unsigned t;
assert( p >= n );
t = x >> ( p - n + 1 );
x >>= ( p - n + 1 );
x ^= ~( ~0 << n );
x = ( x << ( p - n + 1 ) ) | t;
return x;
}
int main()
{
unsigned x = 13;
int p = 4, n = 2;
scanf("%u",&x);
scanf("%d",&p);
scanf("%d",&n);
printf( "%u", invert( x, p, n ) );
return 0;}