Microsoft Interview Question
Software Engineer in TestsCountry: India
Interview Type: Phone Interview
NOTE: ((~0)<<i)^((~0)<<j) ---> will give a mask of type 0001111000 where first 1 from left starts at position j and last 1 is at position i.
{{
unsigned int set(unsigned int n, int i, int j, unsigned int m, int k, int l) {
unsigned mask = ((~0)<<l)^((~0)<<(k));
mask = mask & m; // get the bits between k and l from m
mask = mask >> l; // Removes the trailing 0's
mask = mask << j; // Pushes the bits of interest to left by j
unsigned mask1 = ((~0)<<i)^((~0)<<(j));
mask1 = ~mask1; / Invert the mask
mask1 = mask1 & n; // get all the bits except bits between i and j from n
return (mask1|mask);
}
}}
It can be solved with bitmask .. Firstly we take a copy of bits in n & m and replace them .. here is a simple code
void solve ( int &a , int &b ,int i , int j , int k , int l )
{
vector<int> aa, bb ;
for ( int co=i ; co<=j ; co++ )
aa.push_back( a&(1<<co) ) ;
for ( int co=k ; co<=l ; co++ )
bb.push_back( b&(1<<co) ) ;
for ( int ii=0 ; ii<aa.size() ; ii++ )
{
if ( aa[ii] )
b |= (1<<ii+k) ;
else
b &= ~(1<<(ii+k));
}
for ( int ii=0 ; ii<bb.size() ; ii++ )
{
if ( bb[ii] )
a |= (1<<ii+i) ;
else
a &= ~(1<<(ii+i));
}
return ;
}
void solve(int &a,int &b,int i,int j,int k,int l){
int pa=i,pb;
int tempABit,tempBBit;
for(pa=i,pb=k;pa<=j;pa++,pb++){
tempABit = (1<<pa)&a;
tempBBit = (1<<pb)&b;
tempBBit = tempBBit>>pb;
tempABit = tempABit>>pa;
a = a & (~(1<<pa));
a = a | (tempBBit<<pa);
b = b &(~(1<<pb));
b = b | (tempABit<<pb);
}
}
Another way without bitwise operations:
#include <iostream>
#include <string.h>
#include <sstream>
#include <stdio.h>
#include <math.h>
using namespace std;
int replaceBits (int m, int n, int i, int j, int k, int l) {
int bitsInN = n/(int)(pow(2, k)) % (int)pow(2, l+1);
int bitsInM = m/(int)(pow(2, i)) % (int)pow(2, j+1);
int res = m - (bitsInM-bitsInN) * pow(2,i);
return res;
}
int main() {
printf("%d\n", replaceBits(3072, 170, 3, 5, 5, 7));
return 0;
}
#include<iostream>
#include<math.h>
using namespace std;
int main(void) {
int m;
int n;
cin >> m ;
cin >> n ;
int i,j,k,l;
cin >> i;
cin >> j;
cin >> k;
cin >> l;
int m1, m2, n1, n2, lm1, lm2, ln, ans;
lm1 = (floor(log2(m)) + 1) - i + 1;
lm2 = (floor(log2(m)) + 1) - j;
cout << "lm1 " << lm1 << endl;
m1 = (-1 << lm1) & m;
m2 = (m & (int)(pow(2, lm2) - 1));
ln = (floor(log2(n)) + 1) - l ;
n1 = ((n & ((int)(pow(2, l - k + 1) - 1) << ln)) >> ln) << lm2;
cout << m1 << endl;
cout << m2 << endl;
cout << n1 << endl;
ans = m1 + m2 + n1;
cout << ans << endl;;
return 0;
}
//Simply, I would like to separately get (cut of) the bits from position k to
//position L from number n
//This can be done be shifting number n to the left (31-L) times
//Then Shift to the right ((31-L) + k )times. This will make the number n only
//contains the bits from position k to position L.
//These bits will be at the beginning (right side) of the
//number n. Shifting n to the left i times will position the bits of n in the
//required position of m. Finally do OR operation between m and n.
void Main()
{
uint m = 0x00000C00;
uint n = 0x000000AA;
Console.Write("i: ");
int i = int.Parse(Console.ReadLine());
Console.Write("j: ");
int j = int.Parse(Console.ReadLine());
Console.Write("k: ");
int k = int.Parse(Console.ReadLine());
Console.Write("l: ");
int L = int.Parse(Console.ReadLine());
n <<= (31 - L);
n>>= ((31-L)+k);
n <<= i;
m |= n;
Console.WriteLine("{0:X}", m);
}
I think all those solutions are nice. I, however, will just convert the two numbers to String in java and then swap the digits as the person has asked. Convert it back to number..
- Anonymous September 14, 2011