I am stymied at a part where I require to do the following:
Given a number X (Say 10101010) and a number Y (Say 1110) and two position variables i,j (Say i = 1, j = 4), what I need to do is to set all the bits in X from i to j to match the bits in Y.
For the example above, the answer should be 101|1110|0.
The solution what I had in mind was:
1. Right shift X >> i
3. Run loop from 0 to j-1
2. if(!(X (lsb) ^ Y(lsb)), then continue, else X(lsb) = ~X(lsb)
The thing here is I am not sure how to play with individual bits.
Create a mask where the only zeroes are in bit positions
itoj, which is ~(2j+1 – 2i)Result =
(mask & X) | (Y << i)Example in Python: