02. Copy Set Bits in Range
Last updated
Was this helpful?
Last updated
Was this helpful?
The problem can be found at the following link:
This is a simple bit masking question where we need to generate a mask to filter out specific bits from y
and then perform a bitwise OR operation (x or y
) to copy the values of y
into x
.
To create the mask, follow these steps:
First, determine the length of the filter mask.
Create the mask by left-shifting 1
by the mask length.
Obtain the desired mask by subtracting 1 from the mask and left-shifting it by l - 1
.
Once the mask is ready, apply it to filter out the relevant bits from y
, and perform a bitwise OR operation with x
to copy the filtered values into x
.
Time Complexity : O(1)
, as it does not depend on the size of the inputs x and y.
Auxiliary Space Complexity : constant
as it does not depend on variables
For discussions, questions, or doubts related to this solution, please visit our . We welcome your input and aim to foster a collaborative learning environment.
If you find this solution helpful, consider supporting us by giving a ⭐ star
to the repository.