Wednesday, March 16, 2011

Given two integers A and B. Determine how many bits required to convert A to B. Write a function int BitSwapReqd(int A, int B);

#include "stdio.h"
# define MaxBit 8

int i,nCount=0;

int szA[MaxBit]={0};
int szB[MaxBit]={0};

void main()
{



int nBitSwapReq(int,int);

nBitSwapReq(128,10);

}

int nBitSwapReq(int a,int b)
{

int binaryConversion(int,int *);
binaryConversion(a,szA);
binaryConversion(b,szB);

for(i=0;i<MaxBit;i++)
{
if(szA[i] ^ szB[i])
nCount++;
}
printf("\nnCount == %d\n",nCount);

return 0;
}
int binaryConversion(int xValue,int *szAa)
{
for(i = MaxBit-1; i>=0; i--)
{
if((1<<i)&xValue)
{
printf("1");
*szAa++ = 1;
}
else
{
printf("0");
*szAa++ = 0;
}
}
printf("\n");
return 0;
}

OUTPUT:
10000000 --> 128
00001010 --> 10

nCount == 3
Press any key to continue . . .

No comments:

Post a Comment