## Infosys Interview Question for Software Engineer / Developers

• 0

Comment hidden because of low score. Click to expand.
0
of 0 vote

int BitSwapReqd(int A, int B)
{
unsigned int count;
int diffnum = A ^ B;
for(count=0; diffnum; count++){
diffnum &= diffnum-1;
}

// here count is total no. of bit
}

Comment hidden because of low score. Click to expand.
0

Think this is the most efficient way. Any better ?

Comment hidden because of low score. Click to expand.
0
of 0 vote

The first part to your answer is correct... The XOR bit... However when finding the number of set bits in diffnum. You will have to right shift the num. And why did you declare count be unsigned??? It can be a signed int.

The answer below only works if A ^ B >= 0.
{{
int BitSwapReqd(int A, int B)
{
int count = 0;
int xornum = A ^ B;

while(xornum)
{
//finding if LSB is set. If so increment count
if(xornum & (xornum-1))
count++;
//Right shift to get rid of a bit.
xornum = xornum >> 1;
}
return count;
}

Comment hidden because of low score. Click to expand.
0

I think we need to AND with '1' to figure out if LSB is set or not!

Comment hidden because of low score. Click to expand.
0

Yes we need to AND with '1'

Comment hidden because of low score. Click to expand.
0

Even in the above algo, the part to calculate xornum is corrent and the remaining task is to calculate total no. of set bits in xornum which can be done by checking xornum AND '1' as mentioned in above comment and then right shift the num by 1.

Comment hidden because of low score. Click to expand.
0

What if the 2 integers are represented by different number of bits?

Comment hidden because of low score. Click to expand.
0
of 0 vote

The first solution is right, you dont need to right shift as n&(n-1) makes rightmost bit as 0. So no of times loop runs = number of 1's in the number. Moreover one can use unsigned, not a problem in there. It just increases ur range and, obviously count wont be negative

Comment hidden because of low score. Click to expand.
0
of 0 vote

both are right. note in the first solution, diffnum &= diffnum-1;, which sets diffnum = diffnum & (diffnum-1), making the rightmost bit to 0; in the second solution, instead of "if (xornum & (xornum-1))", if you did "xornum= xornum & (xornum-1)", you would not need the right shift to get rid of the bit.

Comment hidden because of low score. Click to expand.
0
of 0 vote

Here is the code..

``````#include<stdio.h>
void main()
{
int a,b,c,count=0;
printf("Enter the two numbers \n");
scanf("%d", &a);
scanf("%d", &b);
c = a^b;
//Counting the no. of ON bits in c
while(c)
{
count++;
c = c & (c-1);
}
printf("The total no. of transformations required is %d\n",count);
}``````

Comment hidden because of low score. Click to expand.
0

What id the number of bits representing the 2 integers is not same ?
Ex: if suppose one number is represented by 8 bits and the other by 10 bits?

Comment hidden because of low score. Click to expand.
0
of 0 vote

return(bitCount(A^B));

Comment hidden because of low score. Click to expand.
0

unsigned int count=0;
int diffnum = A ^ B;
while (diffnum)
{
if(diffnum & 1)
count++;
diffnum = diffnum >> 1;
}

Comment hidden because of low score. Click to expand.
0
of 0 vote

public class BinaryAnd {

public static void main(String [] args){
int a=10;
int b=7;

//System.out.println(Integer.toBinaryString(a));
//System.out.println(Integer.toBinaryString(b));

int c=a^b;
String s1=Integer.toBinaryString(c);
System.out.println(s1);
int count=0;
for(int i=0;i<s1.length();i++){
if(s1.charAt(i)=='1')count++;

}
System.out.println("The number of bit swaps required are: "+count);
//System.out.println(c);

}

}

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````#include<stdio.h>

using namespace std;
int count(int res)
{
int c=0;
while(res)
{
if(res&1)
c++;
res>>=1;
}
return c;
}
int main()
{
int a=0,b=0;
scanf("%d %d",&a,&b);
int res = a^b;
printf("%d ",count(res));
return 0;
}``````

Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

### Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

### Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.