Given a non-negative number N and two values L and R. The problem is to set all the bits in the range L to R in the binary representation of N.
Example 1:
Input :
N = 17, L = 2, R = 3 
Output :
23
Explanation:
(17)_{10} = (10001)_{2}
(23)_{10} = (10111)_{2}
The bits in the range 2 to 3 in the binary
representation of 17 are set.
Example 2:
Input :
N = 8, L = 1, R = 2 
Output :
11
Explanation:
(8)_{10} = (1000)_{2}
(11)_{10} = (1011)_{2}
The bits in the range 2 to 3 in the binary
representation of 8 are set.
Your Task:
You don't need to read input or print anything. Your task is to complete the function setAllRangeBits() which takes an Integer N as input and returns the answer.
Expected Time Complexity: O(1)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{8}

Source: geeksforgeeks | Difficulty: EASY
Tags: Bit manipulation

Data structures

Starter code: #User function Template for python3

class Solution:
    def setAllRangeBits(self, N , L , R):
        # code here 

Solutions:
class Solution:

	def setAllRangeBits(self, N, L, R):
		for i in range(L - 1, R):
			N = N | 1 << i
		return N


class Solution:

	def setAllRangeBits(self, N, L, R):
		b = bin(N)[2:]
		b = b[::-1]
		t = list(b)
		for i in range(L - 1, R):
			t[i] = '1'
		k = t[::-1]
		s = ''.join(k)
		p = int(s, 2)
		return p


class Solution:

	def setAllRangeBits(self, N, L, R):
		a = bin(N)[2:]
		b = list(a)
		c = b[::-1]
		for i in range(L - 1, R):
			c[i] = '1'
		c = c[::-1]
		s = ''.join(c)
		q = int(s, 2)
		return q


class Solution:

	def setAllRangeBits(self, N, L, R):
		b = bin(N)[2:]
		lst = list(b)
		lst = lst[::-1]
		for i in range(L - 1, R):
			lst[i] = '1'
		lst = lst[::-1]
		q = ''.join(lst)
		b1 = bin(32759720)[2:]
		t = int(q, 2)
		return t


class Solution:

	def setAllRangeBits(self, N, L, R):
		m = pow(2, R)
		n = pow(2, L - 1)
		m = m - n
		return N | m


class Solution:

	def setAllRangeBits(self, N, L, R):
		i = 1
		total = 0
		while N != 0:
			bit = N & 1
			if i >= L and i <= R:
				total = total + 2 ** (i - 1)
				i += 1
				N = N >> 1
			else:
				total = total + bit * 2 ** (i - 1)
				i += 1
				N = N >> 1
		return total


class Solution:

	def setAllRangeBits(self, N, L, R):
		return N | (1 << R) - (1 << L - 1)


class Solution:

	def setAllRangeBits(self, N, L, R):
		x = 2 ** (L - 1) - 1
		y = 2 ** R - 1
		return y - x | N


class Solution:

	def setAllRangeBits(self, N, L, R):
		L -= 1
		num = '1' * (R - L) + '0' * L
		return int(num, 2) | N


class Solution:

	def setAllRangeBits(self, N, L, R):
		num1 = 1 << R
		num1 -= 1
		num2 = 1 << L - 1
		num2 -= 1
		num = num1 ^ num2
		return N | num


class Solution:

	def setAllRangeBits(self, N, L, R):
		val = 2 ** R
		val -= 1
		if L > 1:
			val2 = 2 ** (L - 1)
			val2 -= 1
			val = val ^ val2
		data = val | N
		return data


class Solution:

	def setAllRangeBits(self, N, L, R):
		a1 = 2 ** R
		a1 -= 1
		a2 = 2 ** (L - 1)
		a2 -= 1
		a = a1 ^ a2
		c = N | a
		return c


class Solution:

	def setAllRangeBits(self, N, L, R):
		masklen = (1 << R - L + 1) - 1
		mask = masklen << L - 1
		N = N | mask
		return N


class Solution:

	def setAllRangeBits(self, N, L, R):
		binary = bin(N).replace('0b', '')[::-1]
		arr = list(binary)
		for i in range(L - 1, R):
			arr[i] = '1'
		ans = ''.join(arr)[::-1]
		return int(ans, 2)


class Solution:

	def setAllRangeBits(self, N, L, R):
		l = [0] * (L - 1)
		l.extend([1] * (R - L + 1))
		c = 1
		s = 0
		for i in l:
			if i == 1:
				s += c
			c = c * 2
		return N | s


import sys

class Solution:

	def setAllRangeBits(self, N, L, R):
		int_max = sys.getsizeof(int)
		count = [0] * int_max
		for i in range(int_max):
			if 1 << i & N != 0:
				count[i] = 1
		for i in range(L - 1, R):
			count[i] = 1
		res = 0
		for i in range(int_max):
			if count[i] == 1:
				res += 1 << i
		return res


class Solution:

	def setAllRangeBits(self, N, L, R):
		N = bin(N)[2:]
		N = list(N)
		for i in range(-L, -R - 1, -1):
			N[i] = '1'
		N = ''.join(N)
		return int(N, 2)


class Solution:

	def setAllRangeBits(self, N, L, R):
		N = '{:016b}'.format(N)
		N = N[::-1]
		l = N[:L - 1]
		r = N[L - 1:R]
		r = r.replace('0', '1')
		m = N[R:]
		z = l + r + m
		z = z[::-1]
		return int(z, 2)
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		(N, L, R) = map(int, input().split())
		ob = Solution()
		print(ob.setAllRangeBits(N, L, R))


class Solution:

	def setAllRangeBits(self, N, L, R):
		return N | (1 << R) - 1 - ((1 << L - 1) - 1)
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		(N, L, R) = map(int, input().split())
		ob = Solution()
		print(ob.setAllRangeBits(N, L, R))


class Solution:

	def setAllRangeBits(self, N, L, R):
		power = -1
		num = 0
		val = 0
		while N > 0:
			power += 1
			num += 1
			if L <= num <= R:
				val += 2 ** power
			elif N & 1:
				val += 2 ** power
			N >>= 1
		return val
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		(N, L, R) = map(int, input().split())
		ob = Solution()
		print(ob.setAllRangeBits(N, L, R))


class Solution:

	def setAllRangeBits(self, N, L, R):
		binary_N = ''
		while N >= 1:
			binary_N = str(N % 2) + binary_N
			N = N // 2
		left = len(binary_N) - R
		right = len(binary_N) - L
		for i in range(left, right + 1):
			if binary_N[i] == '0':
				binary_N = binary_N[:i] + '1' + binary_N[i + 1:]
		return int(binary_N, 2)


class Solution:

	def setAllRangeBits(self, N, L, R):
		n = N
		mask = 0
		for i in range(R - L + 1):
			mask = mask | 1
			mask = mask << 1
		mask = mask >> 1
		mask = mask << L - 1
		return mask | n


class Solution:

	def setAllRangeBits(self, N, L, R):
		i = L - 1
		while i <= R - 1:
			if N & 1 << i == 0:
				N = N | 1 << i
			i += 1
		return N


class Solution:

	def setAllRangeBits(self, N, L, R):
		x = list(bin(N))
		for i in range(L, R + 1):
			if i >= 0 and i <= R:
				x[-i] = '1'
		x = ''.join(x)
		x = int(x, 2)
		return x


class Solution:

	def setAllRangeBits(self, N, L, R):
		CN = N
		i = 0
		while CN > 0:
			n = 2 ** i
			if CN & 1 != 1:
				if L <= i + 1 <= R:
					N += n
			CN = CN >> 1
			i += 1
		return N


class Solution:

	def setAllRangeBits(self, N, L, R):
		a = list(bin(N)[2:])
		l = -1 * L
		r = -1 * R
		for i in range(l, r - 1, -1):
			a[i] = 1
		num = 0
		i = len(a) - 1
		for each in a:
			num += int(each) * 2 ** i
			i -= 1
		return num


import math

class Solution:

	def setAllRangeBits(self, N, L, R):
		x = 2 ** R - 2 ** (L - 1)
		return N | x


class Solution:

	def setAllRangeBits(self, N, L, R):
		r = R
		l = L
		ele = (1 << l - 1) - 1 ^ (1 << r) - 1
		return N | ele


class Solution:

	def setAllRangeBits(self, N, l, r):
		for i in range(l, r + 1):
			N = N | 1 << i - 1
		return N


class Solution:

	def setAllRangeBits(self, n, l, r):
		range = (1 << l - 1) - 1 ^ (1 << r) - 1
		return n | range


class Solution:

	def setAllRangeBits(self, N, L, R):
		count = 0
		mask = 1 << R - L + 1
		mask -= 1
		mask <<= L - 1
		N = N | mask
		return N


class Solution:

	def setAllRangeBits(self, N, L, R):
		if L < 0 or R > 32:
			return N
		for i in range(L, R + 1):
			mask = 1 << i - 1
			N = N | mask
		return N


class Solution:

	def setAllRangeBits(self, N, L, R):
		c = 0
		b = 1
		while N > 0:
			a = N % 2
			if b in range(L, R + 1):
				c = c + 2 ** (b - 1)
			else:
				c = c + 2 ** (b - 1) * a
			b = b + 1
			N = N // 2
		return c


class Solution:

	def setAllRangeBits(self, N, L, R):
		temp = 2 ** R - 1 ^ 2 ** (L - 1) - 1
		return N | temp


class Solution:

	def setAllRangeBits(self, N, L, R):
		a = 0
		for i in range(len(bin(N)) - 1):
			if i == L - 1:
				while i != R:
					N |= 1 << i
					i += 1
				a = 1
			if a:
				break
		return N


class Solution:

	def setAllRangeBits(self, n, l, r):
		mask = 1 << r - l + 1
		mask = mask - 1
		mask = mask << l - 1
		return n | mask