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
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.
CPP
#include <bits/stdc++.h>
using namespace std;
void copySetBits(unsigned &x;, unsigned y,
unsigned l, unsigned r)
{
if (l < 1 || r > 32)
return ;
for (int i=l; i<=r; i++)
{
int mask = 1 << (i-1);
if (y & mask)
x = x | mask;
}
}
int main()
{
unsigned x = 10, y = 13, l = 1, r = 32;
copySetBits(x, y, l, r);
cout << "Modified x is " << x;
return 0;
}
|
Java
import java.util.*;
class GFG{
static int copySetBits(int x, int y,
int l, int r)
{
if (l < 1 || r > 32)
return x;
for (int i = l; i <= r; i++)
{
int mask = 1 << (i-1);
if ((y & mask)!=0)
x = x | mask;
}
return x;
}
public static void main(String[] args)
{
int x = 10, y = 13, l = 1, r = 32;
x = copySetBits(x, y, l, r);
System.out.print("Modified x is " + x);
}
}
|
Python3
def copySetBits(x, y, l, r):
if (l < 1 or r > 32):
return x;
for i in range(l, r + 1):
mask = 1 << (i - 1);
if ((y & mask) != 0):
x = x | mask;
return x;
if __name__ == '__main__':
x = 10;
y = 13;
l = 1;
r = 32;
x = copySetBits(x, y, l, r);
print("Modified x is ", x);
|
C#
using System;
public class GFG {
static int copySetBits(int x, int y, int l, int r)
{
if (l < 1 || r > 32)
return x;
for (int i = l; i <= r; i++)
{
int mask = 1 << (i - 1);
if ((y & mask) != 0)
x = x | mask;
}
return x;
}
public static void Main(String[] args) {
int x = 10, y = 13, l = 1, r = 32;
x = copySetBits(x, y, l, r);
Console.Write("Modified x is " + x);
}
}
|
Javascript
<script>
function copySetBits(x , y , l , r)
{
if (l < 1 || r > 32)
return x;
for (i = l; i <= r; i++)
{
var mask = 1 << (i - 1);
if ((y & mask) != 0)
x = x | mask;
}
return x;
}
var x = 10, y = 13, l = 1, r = 32;
x = copySetBits(x, y, l, r);
document.write("Modified x is " + x);
</script>
|
Time Complexity: O(r)
Auxiliary Space: O(1)
Method 2 (Copy all bits using one bit mask)
CPP
#include <bits/stdc++.h>
using namespace std;
void copySetBits(unsigned &x;, unsigned y,
unsigned l, unsigned r)
{
if (l < 1 || r > 32)
return ;
int maskLength = (1ll<<(r-l+1)) - 1;
int mask = ((maskLength)<<(l-1)) & y ;
x = x | mask;
}
int main()
{
unsigned x = 10, y = 13, l = 2, r = 3;
copySetBits(x, y, l, r);
cout << "Modified x is " << x;
return 0;
}
|
Java
import java.util.*;
class GFG{
static int copySetBits(int x, int y,
int l, int r)
{
if (l < 1 || r > 32)
return x;
int maskLength = (int)((1L<<(r-l+1)) - 1);
int mask = ((maskLength)<<(l-1)) & y ;
x = x | mask;
return x;
}
public static void main(String[] args)
{
int x = 10, y = 13, l = 2, r = 3;
x = copySetBits(x, y, l, r);
System.out.print("Modified x is " + x);
}
}
|
Python3
def copySetBits(x, y, l, r):
if (l < 1 or r > 32):
return x;
maskLength = (int) ((1 << (r - l + 1)) - 1);
mask = ((maskLength) << (l - 1)) & y;
x = x | mask;
return x;
if __name__ == '__main__':
x = 10;
y = 13;
l = 2;
r = 3;
x = copySetBits(x, y, l, r);
print("Modified x is " , x);
|
C#
using System;
public class GFG
{
static int copySetBits(int x, int y, int l, int r) {
if (l < 1 || r > 32)
return x;
int maskLength = (int) ((1L << (r - l + 1)) - 1);
int mask = ((maskLength) << (l - 1)) & y;
x = x | mask;
return x;
}
public static void Main(String[] args) {
int x = 10, y = 13, l = 2, r = 3;
x = copySetBits(x, y, l, r);
Console.Write("Modified x is " + x);
}
}
|
Javascript
<script>
function copySetBits(x , y , l , r) {
if (l < 1 || r > 32)
return x;
var maskLength = parseInt( ((1 << (r - l + 1)) - 1));
var mask = ((maskLength) << (l - 1)) & y;
x = x | mask;
return x;
}
var x = 10, y = 13, l = 2, r = 3;
x = copySetBits(x, y, l, r);
document.write("Modified x is " + x);
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Thanks to Ashish Rathi for suggesting this solution in a comment.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
16 Jun, 2022
Like Article
Save Article