## Amazon Interview Question Software Engineer / Developers

• 0

Given two unsigned integers, write an efficient function which returns the no. of bits needs to be flipped of one to generate the other.

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

zhaoyangster that looks correct.
I have one variation/minor improvement to suggest, use c=c&c-1 instead of c=c>>1 and you only loop once for each bit, instead of all the bit positions.
{{
int bit_swaps_required( int a, int b ) {
unsigned int count = 0;
for( int c = a ^ b; c != 0; c = c & c-1 ) {
++count;
}
return count;
}
}}

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

thumb up!

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

saw the solution before: Use XOR

int bit_swaps_required( int a, int b ) {
unsigned int count = 0;
for( int c = a ^ b; c != 0; c = c >> 1 ) {
count += c & 1;
}
return count;
}

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

Your approach is right. XOR the two numbers. And now we need to find number of 1's in this number. There are various efficient ways to find the number of 1's in any number. Just google it.

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

Developer your code is working perfectly.Can u explain the logic behind c=c&c-1?

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

it takes out all the 1's from RHS

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

int num_bits(int x,int y)
{
int a,b;
a=x^y;
b=count_ones(a);
return(b);
}
int count_ones(int x)
{
int count;
while((n&(n-1))!=0)
count++;
return(count);
}

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

``````int bitswap(int a,int b)
{
int c = a^b;
int count = 0;
while(c!=0)
{
if(c&1 ==1)
count++;

c = c>>1;
}
return count;
}``````

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

int num_bits(int x, int y)
{
int count = 0;
while(x != 0 || y != 0)
{
if((x&1)!=(y&1))
{
count++;
}
x = x>>1;
y = y>>1;
}
return count;
}

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

int bit_flip(int x, int y)
{
int count=0;
z=x^y;
while(z)
{
z&=z-1;
count++;
}
return count;
}

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

``````public int flipBits(int a, int b){
int c = a ^ b;
int count =0;
while(x!=0){
if((x&1)==1){
count++;
}
x= (x>>>1);
}
return count;
}``````

it goes like:
1) XOR 2 numbers
2) count the number of 1's

I didn't check it for signed integers. Please correct me if am wrong somewhere.

Name:

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

### Books

is a comprehensive book walking you through every aspect of getting a job at a top tech company, while focuses on software engineering interviews.

### 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.