Saturday, December 26, 2009

No.of bits required to change to convert integer a to b

int BitSwapReqd(int a, int b)
{
int temp1=0;
int temp2=0;
int count=0;

while(a!=0 || b!=0)
{
temp1 = a & 1;
temp2 = b & 1;
a = a>>1;
b = b>>1;
if(temp1 != temp2)
count++;
}
return count;
}

2 comments:

eshwari said...

Sir,

I wish if there was a little explanation provided, conveying how the program works...

because this post would be understood by one who already knows the basics of C and in some cases only after implementing the prog...

If a short explanation and an example data input and output also can be provided; even THE NEW LEARNERS OF C having very little C knowledge also can understand how the program works...

Ignore if you feel it is not required...

With regards,
Eshwari.

Venkat said...

Agree with you, but please send me e-mail for more details, will reply.