VOOZH about

URL: https://www.geeksforgeeks.org/dsa/copy-set-bits-in-a-range/

⇱ Copy set bits in a range - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Copy set bits in a range

Last Updated : 23 Jul, 2025

Given two numbers x and y, and a range [l, r] where 1 <= l, r <= 32. The task is consider set bits of y in range [l, r] and set these bits in x also.
Examples : 

Input : x = 10, y = 13, l = 2, r = 3
Output : x = 14
Binary representation of 10 is 1010 and 
that of y is 1101. There is one set bit
in y at 3'rd position (in given range). 
After we copy this bit to x, x becomes 1110
which is binary representation of 14.

Input : x = 8, y = 7, l = 1, r = 2
Output : x = 11


Source : D E Shaw Interview
 

Recommended Practice


Method 1 (One by one copy bits) 
We can one by one find set bits of y by traversing given range. For every set bit, we OR it to existing bit of x, so that the becomes set in x, if it was not set. Below is C++ implementation.
 


Output
Modified x is 15

Time Complexity: O(r)

Auxiliary Space: O(1)


 
Method 2 (Copy all bits using one bit mask)
 


Output
Modified x is 14

Time Complexity: O(1)

Auxiliary Space: O(1)


Thanks to Ashish Rathi for suggesting this solution in a comment.
 

Comment
Article Tags: