Wednesday, 23 April 2014

9. Write a function iovert( x, p, 0) that returns x with the obits that begin at position p inverted. You can assume that x, p and 0 are integer variables and that the function will return an integer. As an example, if x = 181 [decimal] which is 10110101 in binary, and p = 4 and 0 = 2, then the function will return 101Q!101 or 173 [decimal]. The underlined bits are the changed bits. Note that bit positions are counted from the right to the left and that the counts start with a O. Therefore, position 4 is the 5th bit from the right values



#include<stdio.h>
#include<conio.h>
#include<math.h> main()
{
int invert(int,int,int); int x,n,p;
clrscr();
printf("enter any number x="); scanf("%d",&x);
printf("enter any number n="); scanf("%d",&n);
printf("enter any number p="); scanf("%d",&p);
x=invert(x,p,n);
printf("\n %d",x); getch();
return 0;
}
int invert(int x,int p,int n)
{
int i,ct=0,b[100],y,sum=0; while( x != 1 )
{
y=x%2; b[ct]=y; ct++; x=x/2;
}
b[ct]=x; for(i=ct;i>=0;i--)
printf("%d",b[i]);
for(i=p;n!=0;i--)
{

if ( b[i]==0 )
b[i]=1;
else b[i]=0;
n--;
}
printf("\n");
for (i=ct;i>=0;i--)
printf("%d",b[i]); for(i=0;i<=ct;i++) sum=sum+b[i]*pow(2,i); return sum;
}

 

No comments:

Post a Comment