Given two numbers X and Y, and a range [L, R] where 1 <= L <= R <= 31. Find the set bits of Y in range [L, R] and set these bits in X also. Return this modified X.
Example 1:
Input: 
X = 44, Y = 3 
L = 1,  R = 5
Output: 47
Explaination: Binary represenation of 44 and 3 is 101100 and 11. So in the range 1 to 5 there  are two set bits. If those are set in 44 it will become 101111 which is 47.
Example 2:
Input: 
X = 16, Y = 2
L = 1,  R = 3
Output: 18
Explaination: Binary represenation of 16 and 2 is 10000 and 10. If the mentioned conditions are applied then 16 will become 10010 which is 18.
Your Task:
You do not need to read input or print anything. Your task is to complete the function setSetBit() which takes the numbers X, Y, L and R as input parameters and returns the modified value of X.
Expected Time Complexity: O(R - L)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ X, Y ≤ 10^{9}
1 ≤ L ≤ R ≤ 32

Source: geeksforgeeks | Difficulty: EASY
Tags: Bit manipulation

Data structures

Starter code: #User function Template for python3



class Solution:

    def setSetBit(self, x, y, l, r):

        # code here

Solutions:
class Solution:

	def setSetBit(self, x, y, l, r):
		mask = (1 << r - l + 1) - 1
		mask = mask << l - 1
		mask = mask & y
		x = x | mask
		return x


class Solution:

	def setSetBit(self, x, y, l, r):
		s = 0
		lx = []
		ly = []
		for i in range(0, 32, 1):
			xbit = x & 1 << i
			ybit = y & 1 << i
			if i >= l - 1 and i <= r - 1:
				xbit = xbit | ybit
			s = s + xbit
		return s


class Solution:

	def setSetBit(self, x, y, l, r):
		s = 0
		lx = []
		ly = []
		for i in range(0, 32, 1):
			if x & 1 << i:
				lx.append(1)
			else:
				lx.append(0)
			if y & 1 << i:
				ly.append(1)
			else:
				ly.append(0)
		for i in range(0, 32, 1):
			if lx[i] == 0 and ly[i] == 1:
				if i >= l - 1 and i <= r - 1:
					lx[i] = 1
		for i in range(0, 32, 1):
			s = s + lx[i] * 2 ** i
		return s


class Solution:

	def setSetBit(self, x, y, l, r):
		for i in range(l - 1, r):
			if y & 1 << i:
				x = x | 1 << i
		return x


class Solution:

	def setSetBit(self, y, x, l, r):
		x = x >> l - 1
		itr = l - 1
		while r - l >= 0 and x > 0:
			if x & 1 == 1:
				y = y | pow(2, itr)
			l += 1
			itr += 1
			x = x >> 1
		return y


class Solution:

	def setSetBit(self, x, y, l, r):
		count = 0
		while y > 0:
			now = y % 2
			y //= 2
			count += 1
			if count >= l and count <= r and (now == 1):
				t = x // 2 ** (count - 1)
				if t % 2 == 0:
					x += 2 ** (count - 1)
		return x


class Solution:

	def setSetBit(self, x, y, l, t):
		s = 0
		for i in range(l, t + 1):
			s = s + (1 << i - 1 & y)
		return x | s


class Solution:

	def setSetBit(self, x, y, l, r):
		if l < 1 and r > 32:
			return x
		for i in range(l, r + 1):
			mask = 1 << i - 1
			if mask & y != 0:
				x = x | mask
		return x


class Solution:

	def setSetBit(self, x, y, l, r):
		resp_list = list()
		resp = 0
		left = l - 1
		right = r - 1
		n = 1
		while x != 0 or y != 0:
			bit_x = x & 1
			bit_y = y & 1
			if n >= l and bit_y and (n <= r):
				resp_list.append(bit_y)
				resp += 2 ** (n - 1) * bit_y
			else:
				resp_list.append(bit_x)
				resp += 2 ** (n - 1) * bit_x
			x = x >> 1
			y = y >> 1
			n += 1
		return resp


class Solution:

	def setSetBit(self, x, y, l, r):
		resp_list = list()
		resp = 0
		left = l - 1
		right = r - 1
		n = 1
		while x != 0 or y != 0:
			bit_x = x & 1
			bit_y = y & 1
			if n >= l and bit_y and (n <= r):
				resp_list.append(bit_y)
			else:
				resp_list.append(bit_x)
			x = x >> 1
			y = y >> 1
			n += 1
		for (index, elem) in enumerate(resp_list):
			resp += 2 ** index * elem
		return resp


class Solution:

	def setSetBit(self, x, y, l, r):
		number1 = list()
		number2 = list()
		resp = 0
		left = l - 1
		right = r - 1
		n = x
		while n > 0:
			bit = n & 1
			number1.append(bit)
			n = n >> 1
			if n == 0:
				break
		n = y
		while n > 0:
			bit = n & 1
			number2.append(bit)
			n = n >> 1
			if n == 0:
				break
		while len(number2) <= r:
			number2.append(0)
		while len(number1) <= r:
			number1.append(0)
		while True:
			if left <= right:
				if number2[left] == 1:
					number1[left] = 1
				left += 1
			else:
				break
		for (index, elem) in enumerate(number1):
			resp += 2 ** index * elem
		return resp


class Solution:

	def setSetBit(self, x, y, l, r):
		pos = []
		i = 0
		while y and i < r:
			if y & 1 and i >= l - 1:
				pos.append(i)
			y = y >> 1
			i += 1
		for elt in pos:
			x = x | 1 << elt
		return x


class Solution:

	def setSetBit(self, x, y, l, r):
		l -= 1
		y = bin(y)[2:]
		y = y[::-1]
		y = '0' * l + y[l:r] + '0'
		y = int(y[::-1], 2)
		return x | y


class Solution:

	def setSetBit(self, 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


class Solution:

	def setSetBit(self, x, y, l, r):
		num = 1 << r
		num -= 1
		a2 = 1 << l - 1
		a2 -= 1
		num = num ^ a2
		num = num & y
		x = x | num
		return x


class Solution:

	def setSetBit(self, x, y, l, r):
		i = l
		cnt = 0
		while i <= r:
			if y & 1 << i - 1:
				x = x | 1 << i - 1
			i += 1
		return x


class Solution:

	def setSetBit(self, x, y, l, r):
		if l < 1 and r > 32:
			return x
		range = 2 ** (r - l + 1) - 1 << l - 1
		value = y & range
		x = x | value
		return x


class Solution:

	def setSetBit(self, x, y, l, r):
		z = abs(l - r) + 1
		a = 2 ** z - 1
		a = a << l - 1
		y = y & a
		x = x | y
		return x


import math

class Solution:

	def setSetBit(self, x, y, l, r):
		res = x
		op = 1 << l - 1
		for i in range(l, r + 1):
			if op & y:
				res |= op
			op <<= 1
		return res


import math

class Solution:

	def setSetBit(self, x, y, l, r):
		y = y >> l - 1
		a = 1
		a = a << r - l + 1
		a -= 1
		y = y & a
		y = y << l - 1
		return y | x


class Solution:

	def setSetBit(self, x, y, l, r):
		i = 1
		while y > 0:
			if y % 2 != 0 and i <= r and (i >= l):
				x = x | 1 << i - 1
				y //= 2
			else:
				y //= 2
			i += 1
		return x


from ctypes import c_int32

class Solution:

	def setSetBit(self, x, y, l, r):
		y = y >> l - 1
		y = y << l - 1
		y = y << 32 - r
		mask = (1 << 32) - 1
		y = y & mask
		y = y >> 32 - r
		return x | y


class Solution:

	def setSetBit(self, x, y, l, r):
		k = 1 << l - 1
		i = l
		while i >= l and i <= r:
			if y & k != 0:
				x = x | k
			i += 1
			k = k << 1
		return x


class Solution:

	def setSetBit(self, x, y, l, r):
		mem = (1 << r) - (1 << l - 1)
		return x | mem & y


class Solution:

	def setSetBit(self, x, y, l, r):
		count = 0
		while y > 0:
			if y & 1 > 0 and count >= l - 1 and (count <= r - 1):
				x = x | 1 << count
			count += 1
			y >>= 1
		return x


class Solution:

	def setSetBit(self, x, y, l, r):
		if l < 1 or r > 32:
			return x
		mask_ones = (1 << r - l + 1) - 1
		mask = mask_ones << l - 1 & y
		x = x | mask
		return x


class Solution:

	def setSetBit(self, x, y, l, r):
		data = 0
		for i in range(r - l + 1):
			data <<= 1
			data |= 1
		x |= y & data << l - 1
		return x


class Solution:

	def setSetBit(self, x, y, l, r):
		for i in range(l - 1, r):
			a = 2 ** i
			if y // a % 2 == 1 and x // a % 2 == 0:
				x = (x // a + 1) * 2 ** i + x % a
		return x


class Solution:

	def setSetBit(self, x, y, l, r):
		mask1 = int((1 << r - l + 1) - 1)
		mask1 = mask1 << l - 1
		mask2 = mask1 & y
		x = x | mask2
		return x


class Solution:

	def setSetBit(self, x, y, l, r):
		bit = 1
		while l <= r:
			if y & 1 << l - 1:
				x = x | 1 << l - 1
			l += 1
		return x


class Solution:

	def setSetBit(self, x, y, l, r):
		for k in range(l - 1, r):
			if y & 1 << k != 0:
				mask = 1 << k
				x = x | mask
		return x


class Solution:

	def setSetBit(self, x, y, l, r):
		res = 0
		ref = 1
		for i in range(1, r + 1):
			if i >= l and i <= r and (y & ref != 0):
				x = x | ref
			ref <<= 1
		return res + x


class Solution:

	def setSetBit(self, x, y, l, r):
		for i in range(l - 1, r):
			if y & 1 << i:
				x = x | 1 << i
		return x
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		arr = input().split()
		x = int(arr[0])
		y = int(arr[1])
		l = int(arr[2])
		r = int(arr[3])
		ob = Solution()
		print(ob.setSetBit(x, y, l, r))


class Solution:

	def setSetBit(self, x, y, l, r):
		if l < 1 or r > 32:
			return x
		masklen = (1 << r - l + 1) - 1
		mask = masklen << l - 1 & y
		x |= mask
		return x
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		arr = input().split()
		x = int(arr[0])
		y = int(arr[1])
		l = int(arr[2])
		r = int(arr[3])
		ob = Solution()
		print(ob.setSetBit(x, y, l, r))


class Solution:

	def setSetBit(self, x, y, l, r):
		count = l
		y = y >> count - 1
		while y > 0:
			if count >= l and count <= r:
				if y & 1:
					temp = 2 ** (count - 1)
					x = x | temp
			count += 1
			y = y >> 1
		return x
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		arr = input().split()
		x = int(arr[0])
		y = int(arr[1])
		l = int(arr[2])
		r = int(arr[3])
		ob = Solution()
		print(ob.setSetBit(x, y, l, r))