Microsoft Interview Question for Software Engineer / Developers


Country: India
Interview Type: In-Person




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

Mask all odd bits with 10101010 in binary (which is 0xAA), then shift them left to put them in the even bits. Then, perform a similar operation for even bits. This takes a total 5 instructions.

public static int swapOddEvenBits(int x) {
    return ( ((x & 0xaaaaaaaa) >> 1) | ((x & 0x55555555) << 1) );
}

- Nitin Gupta February 07, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Plz explain dis..... with an example!

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

//Maybe if you break it down like this, it might make sense

int SwapAlternateBits(int n){
int odd = n & 0xAAAAAAAA; //Mask all the odds
int even = n & 0x55555555; //Mask all the evens

int odd_s = odd >> 1; //Shift all odds to the right (this is their alternate spot)
int even_s = even << 1; //Shift all events to the left (This is their alternate spot)

int sol = odd_s | even_s; //Combine both ODDs and EVENs to one number

return sol;
}

- Anonymous February 09, 2013 | Flag
Comment hidden because of low score. Click to expand.
6
of 6 vote

{
var = (((var & 0xAAAAAAAA) > 1) | ((var & 0x55555555) < 1))
}

- Naveen February 07, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

right .... but shift operator is >> and << :-) ..

- rohith August 04, 2013 | Flag
Comment hidden because of low score. Click to expand.
5
of 5 vote

val = ((val & ULONG_MAX / 3) << 1) | ((val & (ULONG_MAX/3)*2) >> 1)

By FindMind - Vaishnavi, NN Priya, Prabhakaran Dhandapani, Sridhar Arumugasamy

- Anonymous July 18, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

# include <stdio.h>

unsigned int swap_bits(unsigned int v) 
{

    // swap odd and even bits
    v = ((v >> 1) & 0x55555555) | ((v & 0x55555555) << 1);
    return v;
}

int main()
{
   printf(" Before swapping %d" , 5);
   printf(" After swapping %d" , swap_bits(5));
   printf(" Before swapping %d" , swap_bits(swap_bits(5)));
   return 0;
}

- Guru February 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hi,
We can also solve this with the following approach:

If you will see closely, then you will find...You have to take care of the following conditions:
Start from MSB.

Take one variable, temp = 0..Which will store the result.
if( Bit is '1' and at Odd position) {
Using this, we have to add the result i.e. temp with the (2 raise to position of bit, by starting the position from 1 for MSB)
}
else if( Bit is '1' and at Even position.) {
Using this, we have to add the result with the (2 raise to position of bit and divide by 2)
}
else { for '0' at even or odd, will not help}

At the end return the final result i.e. temp.

Please correct me, If you finding it wrong.

Thanks..

- Manpreet February 07, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>
int bit_swap(int);
int power(int);
int main()
{
int a=15;
printf("%d",bit_swap(a));
}
int bit_swap(int a)
{
int k=3,op1,op2,out=0;
while(k>0)
{
op1=(a&power(k))>>k;
op2=(a&power(k-1))>>k-1;
out|=op1*power(k-1);
out|=op2*power(k);
k-=2;
}
return out;

}
int power(int a)
{
if(a==0)
return 1;
else
return 2*power(a-1);
}

- anvesh February 07, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

var swapBits = function (binNum) {
	var mask1 = parseInt('1010', 2);
	var mask2 = parseInt('0101', 2);
	return ((binNum << 1) & mask1) | ((binNum >> 1) & mask2)
}

- Kartik Bansal February 07, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

//use two arrays.
		//binArr will hold bit pattern
		int[] bitArr = {8,4,2,1};
		int[] binArr = new int[4];
		
		int num=8;
		//create bit pattern
		for (int i = 0; i < bitArr.length; i++) {
			
			if(bitArr[i]<=num && num>=0){
				num=num-bitArr[i];
				binArr[i]=1;
			}else{
				binArr[i]=0;
				//i++;
			}
		}
		
		
		
		//reverse bit
		
		
		int temp=binArr[0];
		binArr[0]=binArr[1];
		binArr[1]=temp;
		temp=binArr[2];
		binArr[2]=binArr[3];
		binArr[3]=temp;
		
		
		
		
		
		int revNum=0;
		for (int i = 0; i < binArr.length; i++) {
		
			if(binArr[i]==1)
				revNum=revNum+bitArr[i];
			
		}

		
		System.out.println("rev num:"+revNum);

- suvrokroy February 08, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Even this method helps, if we aren't allowed to use and hex-numbers.

int bits = 8;
		for(int i=0;i<bits;i+=2){
			int multiplier_1 = (int) Math.pow(2, i);
			int multiplier_2 = (int) Math.pow(2, i+1);
			int result_1 = number & multiplier_1;
			int result_2 = number & multiplier_2;
			
			if(result_1 != result_2 && (result_1 ==0 || result_2 ==0)){
				if(result_2 > result_1)
					number = (int) (number - Math.pow(2, i));
				else
					number = (int) (number + Math.pow(2,i));
			}
		}

- Hitman February 09, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

simple question:

public class SwapAdjacentBit {

	public static int swapBit(int n){
		int odds = n & 0xAAAAAAAA;
		int evens = n & 0x55555555;
		
		return odds >> 1 | evens << 1;
		
	}
	
	public static void main(String[] args) {
		System.out.println(swapBit(4));
	}
}

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

void main()
{
	int a=1,b,k,l,t,i;
	printf("Enter number");
	scanf("%d",&b);

	for(i=0;i<4;i++)
	{
		t=a;
		k=a&b;
		a=a*2;
		l=a&b;
		if(k!=l)
		{
			b=b^t;
			b=b^a;
		}
		a=a*2;

	}

	printf("Number is %d",b);
}

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

#include<stdio.h>
int main()
{
int n;
printf("enter the n value");
scanf("%d",&n);
printf("%d",(((n&0xaaaaaaaa)>>1)|((n&55555555)<<1))));
return 0;
}

- ram rs April 19, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

static void PrintSwapAlternateBitsofGivenNumber(int t)
{
int remainder = 0;
int divided = t;
int count = 0;
int digit = 1;
Stack q = new Stack();

while (divided > 0)
{
remainder = divided % 2;
q.Push(remainder);
divided = divided / 2;
if (divided == 0)
{
q.Push(divided);
}
count++;
}

int sum = 0;
while (count >= 0)
{
sum = sum + q.Pop() * digit;
digit = digit * 2;
count--;
}

Console.WriteLine(sum);
}

- jinzha May 16, 2013 | 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