编写inverse_n函数,返回将无符号整数x的第pos为开始的n位取反后的值。
1、
#include <stdio.h>
unsigned inverse_n(unsigned x, int pos, int n)
{
int i;
for(i = pos; i <= pos + n - 1; i++)
{
if(x >> i & 1U)
x = (x & (~(1 << i)));
else
x = (x | 1 << i);
}
return x;
}
int main(void)
{
unsigned x;
int pos, i;
puts("please input three integers.");
printf("x = "); scanf("%u", &x);
printf("pos = "); scanf("%d", &pos);
printf("i = "); scanf("%d", &i);
printf("result: %u
", inverse_n(x, pos, i));
return 0;
}