GeeksforGeeks » Interview Questions
Adobe Interview Question for Software Engineer/Developer (Fresher) about Bit Magic
(11 posts)-
You are given two 32-bit numbers, N and M, and two bit positions, i and j. Write a method to set all bits between i and j in N equal to M (e.g., M becomes a substring of N located at i and starting at j).
EXAMPLE:
Input: N = 10000000000, M = 10101, i = 2, j = 6
Output: N = 10001010100
_
________________________________________________________________ -
#include<stdio.h> #include<stdlib.h> int main() { int N,M,i,j; printf("Enter value of N \n"); scanf("%d",&N); fflush(stdin); printf("Enter value of M \n"); scanf("%d",&M); fflush(stdin); printf("Enter value of i \n"); scanf("%d",&i); fflush(stdin); printf("Enter value of j \n"); scanf("%d",&j); fflush(stdin); int a=0,k; for( k=0;k<j;k++) { a= a<<1; a=a|1; } for(k =0;k<i;k++) { a=a<<1; } N = N &(~a); printf("value of N is %d",N); for(k=0;k<i;k++) M=M<<1; N=N|M; printf("value of N is %d",N); getchar(); } -
@raj Cool can You Please explain the Algo & Logic ...Waiting for your explanation
-
Algo is
1) clear j bits of N starting from ith bit
2)left shift M by i
3) OR N with M
N is the required result -
How about this ?
1. Prepare a mask with bits set from i to j,
a. Assign integer number with all bits set to 1, i.e. 0xFFFF FFFF
b. Shift left i times and shift back to set first i bits to zero.
c. Shift right 32 - j times and shift back to set last 32 - j bits to zero.
For e.g. if i = 9 and j = 16 then in binary mask would be 0000 0000 1111 1111 0000 0000 0000 0000. get it ?unsigned int Mask = (unsigned int)-1; // or ~0 Mask = ((Mask << i) >> ( i + (32 - j) )) << j
2. Prepare second mask, 1's compliment of first mask. This would be to restore the bits except i to j in M
Mask2= ~Mask
3. Use the masks to do bitwise and operation with N to extract the bits.
M = ( (N & Mask) | (M & Mask2))
----
iShashank -
How about this logic:
Temp = 1 << (j – I +1) – 1 11111
Temp <<= << i; 1111100
~Temp 0000011
N & Temp;
N | (M << 2) -
Temp = 1 << (j – I +1) – 1
Temp <<= i;
~Temp
N & Temp;
N | (M << 2) -
@rajcools I think your first loop to set 1s should be for( k=0; k<j-i; k++)
-
-
here are the assumption about this algo , as i found some details are missing in the given ques:-
1) (i-j)+1 gives the number of bits to be added int the N . now bcoz M is supposed to be subString of N , hence number of bits in M = (i-j)+1; //right=-1;
right=right<<j;
left=pow(2,i)-1; // this is to extract bits from position 0 to i-1 from N.
if(left!=0)
{
left=n&left; // extracting bits from 0 to i-1 from N.
}
m=m<<i; // shifting m by i , so that we can include it in correction location of N.
n=n&right; // mave all bits to 0 string from position 0 to j
n=n|m; // including M to N.
n=n|left; // incuding left extracted before to N.
printf("\nresult = %d\n\n",n);example :-
Input: n= 11100000010, m = 10101, i = 2, j = 6right=right<<j; // right = 11110000000
left=pow(2,i)-1; // left = 11
if(left!=0)
{
left=n&left; // left = 10
}m=m<<i; // m = 1010100
n=n&right; // n = 11100000000
n=n|m; // n= 11101010100
n=n|left; // n= 11101010110value = 11101010110
-
I would suggest this approach..
1> shift left 1 (j-i+1) times followed by subtracting 1 from it.
2> shift left the result (step 1) i times.
3> Take bitwise complement.
4> AND it with N to set ith to jth bits of N as 0.
5> OR the result with M after left shifting M i times.The one line approach can be implemented as :
N=( N&( ~( ( (1<<(j-i+1)-1) ) <<i) ) ) | (M<<i)
Reply
You must log in to post.