02. Copy Set Bits in Range
The problem can be found at the following link: Question Link
My approach
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 withx
to copy the filtered values intox
.
Example
Time and Auxiliary Space Complexity
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
Code (C++)
Contribution and Support
For discussions, questions, or doubts related to this solution, please visit our discussion section. 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 getlost01/gfg-potd repository.
Last updated