Microsoft Interview Question for Interns


Country: India
Interview Type: Written Test




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

N = (N & 0xAAAA)>>1 | (N & 0x5555)<<1

- loveCoding October 24, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Errrr... 0xAAAA and 0x5555 are 16 bits, the question was for 32 bits

- Selmeczy, Péter October 25, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

For 32 bit : N = (N & 0xAAAAAAAA)>>1 | (N & 0x55555555)<<1

- Amit October 25, 2012 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 vote

With comments:

                int x = 33;			
		int res1 = (x & 0xAAAAAAAA) >> 1; // clear every even bit
		res1 >>= 1; // shift right 
		
		int res2 = x & 0x55555555; // clear every odd bit
		res2 <<= 1; // shift left
		
		int res = res1 | res2;
		
		System.out.println( Integer.toBinaryString(x) );
		System.out.println( Integer.toBinaryString(res) );

- m@}{ October 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

can anyone explain how?

- guest October 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

With comments:

int x = 33;			
		
		int res1 = (x & 0xAAAAAAAA); // clear every even bit
		res1 >>= 1; // shift right 
		
		int res2 = x & 0x55555555; // clear every odd bit
		res2 <<= 1; // shift left
		
		int res = res1 | res2;
		
		System.out.println( Integer.toBinaryString(x) );
		System.out.println( Integer.toBinaryString(res) );

- m@}{ October 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here is one option -
int Reverse(int Data)
{
int i,j;
int res = 0;
for(i = 0 ,j =0; i < 32;i++)
{
if((i%2) == 0) // even
{
j = i +1;
}
else
{
j = i -1 ;
}

res |= (((Data >> i) & 0x01 ) << j);

}
return res;
}

- Amit October 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int swapOddEven(int num)
{
return (num&2863311530)/2 + (num&1431655765)*2;
}

- avico81 November 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

dafaq is 2863311530 and 1431655765 :P

- wooka November 03, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

unsigned int Swap_Bits(unsigned int N)
{
	unsigned temp1,temp2;
	unsigned mask = 0x55555555;
	temp1 = ((N&mask)<<1);//Move bit 1,3,5...31 to 2,4,...32
	temp2 = ((N&(mask<<1))>>1);//Move bit 2,4,6 ... 1,3,5...
	return (temp1 | temp2 );
}

- Rohit Athavale November 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

unable to understand the logic....
can anyone of you explain???

- etilist November 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

0xAAAAAAAA is a hexadecimal number
its binary representation would be
1010 1010 1010 1010 1010 1010 1010 1010
(each A = 1010 in binary, so a total of 8 such bits)
when you do a bit wise & of this with any 32 bit number, it results in all the odd number bits masked to 0. now u RIGHT shift the result by 1 => moving the bits in even number position to odd position

similarly
0x55555555
(0101 0101 0101 0101 0101 0101 0101 0101)
when you do a bit wise & with any 32 bit number, it results in all the odd number bits masked to 0. now you LEFT shift by 1 => moving the odd number position to even number position

now you combine the result. you get the odd-even swapped result of the given number

- Kary December 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Correction:
similarly
0x55555555
(0101 0101 0101 0101 0101 0101 0101 0101)
when you do a bit wise & with any 32 bit number, it results in all the even number bits masked to 0. now you LEFT shift by 1 => moving the odd number position to even number position

- Kary December 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class SwapBit {
	//num is unsigned, 32 bit
	public static int swapBit(int num){
		int odds = (0xAAAAAAAA & num) >> 1;
		int evens = (0x55555555 & num) << 1;
		return odds | evens;
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		System.out.println(swapBit(5));
	}

}

- Kevin March 03, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

What is the need of masking?
We can simple left shift and right and finally do OR operation.

- shiv April 14, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

it is wrong.

- skg October 01, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I tried your logic it is not working.

- skg October 01, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

void main()
{
int n,i,j,r,s,m=0;
printf("enter the 32 bit integer(i.e 8digits)");
scanf("%d",&n);
r=n;
while(r!=0)
{
r=r%10;
s=r%10;
m=m*100+s*10+r;
r=r/10;
}
printf("%d",m);
}

- xyz October 14, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

18 is the answer

- Rajesh August 01, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

18 is the answer

- Rajesh August 01, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
int swapBits(int x)
{
unsigned int even_bit=0xAAAAAAAA&x;
unsigned int odd_bit=0x55555555&x;
return (even_bit>>1)|(odd_bit<<1);
}
int main()
{
int x= 2555;
x= swapBits(x);
print("%d",x);
}

- sandeepkumar211221 December 11, 2020 | 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