Microsoft Interview Question
Software Engineer / DevelopersCountry: India
Interview Type: In-Person
//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;
}
# 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;
}
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..
#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);
}
//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);
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));
}
}
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);
}
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.
- Nitin Gupta February 07, 2013