Infosys Interview Question for Software Engineer / Developers






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
}

- brijesh kumar jaiswal April 11, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Think this is the most efficient way. Any better ?

- Balaji May 16, 2009 | Flag
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;
}

- Anonymous June 06, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- Anonymous August 01, 2007 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes we need to AND with '1'

- Anonymous February 06, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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.

- Sanjay April 04, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- Anonymous May 06, 2015 | Flag
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

- Ani August 11, 2007 | Flag Reply
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.

- anon September 03, 2007 | Flag Reply
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);
}

- gauravk.18 March 13, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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?

- Arushi May 06, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

return(bitCount(A^B));

- mail2vcp@gmail.com October 02, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- sachin October 02, 2008 | Flag
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);




}

}

- badalrocks January 23, 2009 | Flag Reply
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;
}

- rg462012 November 23, 2012 | Flag Reply


Add a Comment
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.

Learn More

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.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More