Adobe Interview Question
Software Engineer / DevelopersIt wont work unless n does not have any set bit withing the segment to be replaced by m.
this works for 8 bit extrapolate for 32.
int BitWise(unsigned char N , unsigned char M, int i , int j)
{
unsigned char m1,m2;
m1 = M;
m1 = m1 >> (8 - j) ;
m1 = m1 << j;
m1 = m1 >> i;
m2 = 0xFF >> (8 - i );
m2 = 0xFF << (8 -i );
m2 = m2 | m1;
unsigned char temp = (0xFF >> j );
m2 = m2 | temp;
unsigned char soln;
soln = ((N | m1) & m2);
return 0;
}
int updateBits(int n, int m, int i, int j) {
int max = ~0; /* All 1’s */
// 1’s through position j, then 0’s
int left = max - ((1 << j) - 1);
// 1’s after position i
int right = ((1 << i) - 1);
// 1’s, with 0s between i and j
int mask = left | right;
// Clear i through j, then put m in there
return (n & mask) | (m << i);
}
public static void converter(int n, int m, int ii, int jj){
String s = Integer.toBinaryString(n);
int length = s.length();
int max = 0;
for(int i=0; i<length; i++){
max = (int) (max + (1 * Math.pow(2, i)));
}
m = m&max;
int left = (max << jj)&max;
int right = (max << (ii-1))&max;
int x = (left^right)&max;
int xneg = (x ^ max)&max;
int a = (n & xneg)&max;
int b = (m & x)&max;
int result = (a | b);
String sres = Integer.toBinaryString(result);
System.out.print("\nthe binary representation of after merging is : "+sres);
}
public static int updateBits(int n, int m, int i, int j) {
int max = ~0; /* All 1’s */
// 1’s through position j, then 0’s
int left = max - ((1 << (j+1)) - 1); // here should be j+1 not j.
// 1’s after position i
int right = ((1 << i) - 1);
// 1’s, with 0s between i and j
int mask = left | right;
// Clear i through j, then put m in there
return (n & mask) | (m << i);
}
int subNum (int N, int M, int i, int j)
{
int mask;
while (i <= j)
{
mask = ~ (1 << i) ;
M = ~ M & (1 << i);
N = N & mask ;
i++;
}
N = N | M;
return N;
}
<pre lang="" line="1" title="CodeMonkey28475" class="run-this">/*
- Anonymous April 29, 2011Three assumptions not mentioned in the original question.
1. M must be not larger than N.
2. j must be high enough to hold M inside N.
3. i can not be any number, but derived from M and j.
*/
public class BinarySequenceImplant {
public static int implant(int n, int m, int startingPos) {
assert n >= m : "m can not be larger than n";
int msbInM = (int) Math.ceil(Math.log10(m) / Math.log10(2) + 0.001);
assert startingPos >= msbInM : "Starting position is too low to implant m";
int mask = (1 << msbInM) - 1 << startingPos - msbInM;
return n & (~mask) | m << startingPos - msbInM;
}
public static void main(String[] args) {
Random rm = new Random();
int n = 10000000 + rm.nextInt(10000000);
int m = 500 + rm.nextInt(500);
int startingPos = 15;
int shift = startingPos
- (int) Math.ceil(Math.log10(m) / Math.log10(2) + 0.001);
int res = implant(n, m, startingPos);
System.out.println(" n: "
+ String.format("%32s", Integer.toBinaryString(n)));
System.out.println(" m: "
+ String.format("%" + (32 - shift) + "s",
Integer.toBinaryString(m)));
System.out.println("result: "
+ String.format("%32s", Integer.toBinaryString(res)));
}
}
</pre>