Given two 32-bit numbers, N and M, and two-bit positions, i and j. Write a method to insert M into N such that M starts at bit j and ends at bit i. You can assume that the bits j through i have enough space to fit all of M. Assuming index start from 0.
a) N = 1024 (10000000000), M = 19 (10011), i = 2, j = 6 Output : 1100 (10001001100) b) N = 1201 (10010110001) M = 8 (1000) i = 3, j = 6 Output: 1217 (10011000001)
This problem has been already discussed in the previous post. In this post, a different approach is discussed.
Approach: The idea is very straightforward, following is the stepwise procedure –
- Capture all the bits before i in N, that is from i-1 to 0.
- We don’t want to alter those bits so we will capture them and use later
- Clear all bits from j to 0
- Insert M into N at position j to i
- Insert captured bits at there respective position ie. from i-1 to 0
Let’s try to solve example b for a clear explanation of the procedure.
Capturing bits i-1 to 0 in N
Create a mask whose i-1 to 0 bits are 1 and rest are 0. Shift 1 to i position in left and subtract 1 from this to get a bit mask having i-1 to 0 bits set.
capture_mask = ( 1 << i ) - 1
So for example b, mask will be –
capture_mask = ( 1 << 3 ) - 1 Now capture_mask = 1(001)
Now AND this mask with N, i-1 to 0 bits will remain same while rest bits become 0. Thus we are left with i-1 to 0 bits only.
captured_bits = N & capture_mask
for example b, captured bits will be –
captured_bits = N & capture_mask Now capture_mask = 1 (001)
Clearing bits from j to 0 in N
Since bits have been captured, clear the bits j to 0 without loosing bits i-1 to 0. Create a mask whose j to 0 bits are 0 while the rest are 1. Create such mask by shifting -1 to j+1 position in left.
clear_mask = -1 << ( j + 1 )
Now to clear bits j to 0, AND mask with N.
N &= clear_mask
For example b will be:
N &= clear_mask Now N = 1152 (10010000000)
Inserting M in N
Now because N has bits from j to 0 cleared, just fit in M into N and shift M i position to left to align the MSB of M with position j in N.
M <<= i
For example b-
M <<= 3 Now M = 8 << 3 = 64 (1000000)
Do a OR of M with N to insert M at desired position.
N |= M
For example b –
N |= M Now N = 1152 | 64 = 1216 (10011000000)
Inserting captured bits into N
Till now M has been inserted into N. Now the only thing left is merging back the captured bits at position i-1 to 0. This can be simply done by OR of N and captured bits –
N |= captured_bits
For example b –
N |= captured_bits N = 1216 | 1 = 1217 (10011000001)
So finally after insertion, the below bitset is obtained.
N(before) = 1201 (10010110001) N(after) = 1201 (10011000001)
Below is the implementation of the above approach:
N = 1201(10010110001) M = 8(1000) After inserting M into N from 3 to 6 N = 1217(10011000001)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Inserting m into n such that m starts at bit j and ends at bit i.
- Minimum bit flips such that every K consecutive bits contain at least one set bit
- Check whether the binary equivalent of a number ends with "001" or not
- Maximize XOR subsequence possible by equidistant elements from both ends
- Check whether the binary equivalent of a number ends with given string or not
- Largest number M having bit count of N such that difference between their OR and XOR value is maximized
- Maximize the expression (A AND X) * (B AND X) | Bit Manipulation
- Convert numbers into binary representation and add them without carry
- Split a binary string into K subsets minimizing sum of products of occurrences of 0 and 1
- Check if left and right shift of any string results into given string
- Partition of a set into K subsets with equal sum using BitMask and DP
- Number of ways to swap two bit of s1 so that bitwise OR of s1 and s2 changes
- Set, Clear and Toggle a given bit of a number in C
- Count of even and odd set bit with array element after XOR with K
- Count of pairs having bit size at most X and Bitwise OR equal to X
- Count of even and odd set bit Array elements after XOR with K for Q queries
- Count of binary strings of length N with even set bit count and at most K consecutive 1s
- Absolute difference between set and unset bit count in N
- Count pairs (A, B) such that A has X and B has Y number of set bits and A+B = C
- Total pairs in an array such that the bitwise AND, bitwise OR and bitwise XOR of LSB is 1
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.