lets say-
if a=1 1 0 0 1 0 1 1
b=0 1 1 1 0 1 1 1
by finding xor of a & b, u will be able to find how many bits in a are reqd
to be changed to get b
bcoz if bits in a & b are different at any position then only u need to
change( 0->1 or 1->0) else not
code
int bitswapreqd(int a, int b)
{
int c, count;
c=a^b;
//now counting the no. of 1's in c
if(c==0) //if both no's are same
return 0;
while(c!=0)
{
c=c & (01);
if(c==1)
count++;
c>>1;
}
return count;
}
On Fri, Jul 29, 2011 at 4:03 AM, Tim Zheng <[email protected]> wrote:
> int BitFlipRequired::bitFlipRequired(int A, int B){
> int count(0);
> int bitMask(1);
>
> A ^= B;
>
> for (unsigned int bitPosition = 0; bitPosition < sizeof(int)*8;
> bitPosition++){
> if ((A & bitMask) != 0)
> count++;
> bitMask <<= 1;
> }
> return count;
> }
>
>
> On Jul 28, 2:43 pm, sylvester <[email protected]> wrote:
> > i just read this quetion somewhere...i dont have any idea what exactly
> > they mean by this...so m looking for any soln that can be possible....
> >
> > On Jul 29, 2:19 am, Tim Zheng <[email protected]> wrote:
> >
> >
> >
> >
> >
> >
> >
> > > What's the basic operation on A allowed here to convert it to B? Just
> > > to flip individual bit?
> >
> > > On Jul 28, 2:14 pm, sylvester <[email protected]> wrote:
> >
> > > > Given two integers A & B. Determine how many bits required to convert
> > > > A to B. Write a function int BitSwapReqd(int A, int B);
>
> --
> You received this message because you are subscribed to the Google Groups
> "Algorithm Geeks" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>
>
--
You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/algogeeks?hl=en.