#include <stdio.h>
#include <math.h>
#define Funa(a,b,c)\
{\
a=a*b;\
c=a-b;\
}
void Funa2(int a,int b,int c)
{
a = a*b;
c = a-b;
}
int invert(int x,int p,int n)
{
int bin_c[16],tmp_c[16];
int i,j;
for(i = 0;i<16;i++)//将十进制数转换为二进制数
{
bin_c[i]=x%2;
x = x/2;
}
printf("the relevant binary number:");
for(i=15;i>=0;i--)//取反前显示
{
printf("%d",bin_c[i]);
}
p = 16-p;//初始为逆向存储,所以定位到首元素开始计算位置
while(n)
{/*对从p位开始的二进制位取反*/
bin_c[p] ^= 1;
--n;
p--;
}
printf("\nafter oppos ,binary number:");
for(i=15;i>=0;i--)//取反后显示
{
printf("%d",bin_c[i]);
}
int x_sum=0;
for(i = 15,j=0;i>=0;i--)//将取反后的二进制数转换为十进制
{
if(bin_c[i]==1){
x_sum += pow(2,i);//2的i次方
}
}
return x_sum;
}
int main()
{
/*
int a=2,b=2,c=1;
Funa(a,b,c);
printf("%d,%d\n",a,c);
Funa2(a,b,c);
printf("%d,%d\n",a,c);
*/
int x,p,n;
printf("please input number:");
scanf("%d,%d,%d",&x);
printf("input the locate and len:");
scanf("%d,%d",&p,&n);
printf("\nafter invert x = %d\n",invert(x,p,n));
return 0;
}
|