Bit Insertion

Easy
0/40
Average time to solve is 15m
profile
Contributed by
21 upvotes
Asked in companies
FacebookD.E.Shaw

Problem statement

You are given two 32 bit positive integers 'X' and 'Y', and two positions 'A' and 'B'. Your task is to insert 'Y' into 'X' starting from A’th position to B’th position both inclusive.

Inserting a number is resetting all the bits of 'X' from position 'A' to position 'B' and then writing 'Y' in 'X' starting from index 'A'.

Note : Bit positions are 0 indexed.


For example :
X = 1536 and Y = 7, A = 1 and B = 5,
X is base 2 = 11000000000, Y in base 2 = 0111
First, we clear all the bits of X from index 1 to index 5 then wrote Y in X starting from A.
After inserting Y in X starting from position result will be 11000001110
Detailed explanation ( Input/output format, Notes, Images )
Input Format :
The first line of input contains an integer 'T’ denoting the number of test cases to run. Then the test case follows.

The first and the only line of each test case contain four single space-separated integers ‘X’, ‘Y’, ‘A’, and ‘B’ denoting the integers and the starting and the ending positions for insertion, respectively. 
Output Format :
For each test case print the number 'X' after inserting the 'Y' from A’th to B’th position.
Output for each test case is printed in a separate line.
Note :
You don’t need to print anything. It has already been taken care of. Just implement the given function.
Constraints :
1 <= T <= 5000
1 <= X,Y <= 2^31
0 <= A <= B < 32 

‘X’, ‘Y’, ‘A’, ‘B’ are two given integers and starting bit positions and ending bit position to insert respectively.
It is guaranteed that the position from ‘A’ to  ‘B’ is enough to insert Y.
Time Limit : 1 sec
Sample Input 1 :
2
1 8 5 15
7 21 11 30
Sample Output 1 :
257
43015
Explanation of Sample Input 1 :
X and Y in base 2 are (00001), (01000), after clearing all the bits in X from 5’th to 15’th position and inserting Y we get (1000000001)
X and Y in base 2 are (111), (10101), after clearing all the bits in X from 11’th to 30’th position and inserting Y we get (0001010100000000111)
Sample Input 2 :
2
12143435 564 1 10 
21321454 129 11 30
Sample Output 2 :
12143721
265966
Hint

Can we use some bitwise operations to solve this problem?

Approaches (1)
Bit Masking

The key observation here is that we can set all the bits in X to 0 from A’th bit to B’th bit using some bitwise operations. Then we finally shift Y by A bits and take the bitwise or with X.

The algorithm will be-

  1. Create a MASK with the first B - A + 1 bits set.
  2. Shift   MASK by A bits to left.
  3. Negate the MASK and take bitwise and with X to clear all set bits of X from B to A.
  4. Shift Y by A bits and take its bitwise or with X. We finally return X.
Time Complexity

O(1)
 

As all the bit operations performed takes constant time the overall time complexity will be O(1).

Space Complexity

O(1) 
 

Constant space is used. 

Code Solution
(100% EXP penalty)
Bit Insertion
Full screen
Console