Given a non-negative number N and two values L and R. The problem is to toggle the bits in the range L to R in the binary representation of N, i.e, to toggle bits from the rightmost Lth bit to the rightmost Rth bit. A toggle operation flips a bit 0 to 1 and a bit 1 to 0. Print N after the bits are toggled.
 
Example 1:
Input:
N = 17 , L = 2 , R = 3
Output:
23
Explanation:
(17)_{10} = (10001)_{2}.  After toggling all
the bits from 2nd to 3rd position we get
(10111)_{2} = (23)_{10}
Example 2:
Input:
N = 50 , L = 2 , R = 5
Output:
44
Explanation:
(50)_{10} = (110010)_{2}.  After toggling all
the bits from 2nd to 5th position we get
(101100)_{2} = (44)_{10}
 
Your Task:
You don't need to read input or print anything. Your task is to complete the function toggleBits() which takes 3 Integers N,L and R as input and returns the answer.
 
Expected Time Complexity: O(1)
Expected Auxiliary Space: O(1)
 
Constraints:
1 <= N <= 10^{5}
1 <= L<=R <=^{ }Number of Bits in N

Source: geeksforgeeks | Difficulty: EASY
Tags: Bit manipulation

Data structures

Starter code: #User function Template for python3



class Solution:

    def toggleBits(self, N , L , R):

        # code here 

Solutions:
class Solution:

	def toggleBits(self, N, L, R):
		self.N = N
		self.L = L
		self.R = R
		nn = bin(N)[2:]
		l_n = list(nn)
		fin = len(l_n) - L + 1
		ini = len(l_n) - R
		for i in range(ini, fin):
			if l_n[i] == '1':
				l_n[i] = '0'
			else:
				l_n[i] = '1'
		r = ''.join(l_n)
		return int(r, 2)


class Solution:

	def toggleBits(self, N, L, R):
		b = bin(N)[2:]
		b1 = list(b)
		for i in range(-L, -R - 1, -1):
			if b1[i] == '0':
				b1[i] = '1'
			else:
				b1[i] = '0'
		k = ''.join(b1)
		return int(k, 2)


class Solution:

	def toggleBits(self, N, L, R):
		num = (1 << R) - 1 ^ (1 << L - 1) - 1
		return N ^ num


class Solution:

	def toggleBits(self, N, L, R):
		for i in range(L, R + 1):
			N = N ^ 1 << i - 1
		return N


class Solution:

	def toggleBits(self, N, L, R):
		l = 1 << L - 1
		r = 1 << R - 1
		m = r - l
		toggle = l | r | m
		return N ^ toggle


class Solution:

	def toggleBits(self, N, L, R):
		rcode = (1 << R) - 1
		lcode = (1 << L - 1) - 1
		return N ^ rcode ^ lcode


class Solution:

	def toggleBits(self, N, L, R):
		i = 0
		while R >= L:
			i += 1 << R - 1
			R -= 1
		return N ^ i


class Solution:

	def toggleBits(self, N, L, R):
		res = 0
		i = 0
		while N > 0:
			j = N % 2
			if i >= L - 1 and i <= R - 1:
				if j == 0:
					res = res + 2 ** i
			else:
				res = res + j * 2 ** i
			i += 1
			N = N // 2
		return res


class Solution:

	def toggleBits(self, N, L, R):
		res = (1 << L - 1) - 1
		res1 = (1 << R) - 1
		temp = res ^ res1
		return N ^ temp


class Solution:

	def toggleBits(self, N, L, R):
		le = R - L + 1
		a = 1 << le
		num = a - 1
		num = num << L - 1
		return N ^ num


class Solution:

	def toggleBits(self, N, L, R):
		a = R - L
		n = (1 << a + 1) - 1
		n = n << L - 1
		return N ^ n


class Solution:

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


class Solution:

	def toggleBits(self, N, L, R):

		def reversebit(a):
			if a == 1:
				return 0
			else:
				return 1
		s = list(map(int, str(bin(N))[2:]))
		s = s[::-1]
		n = len(s)
		for i in range(L - 1, R):
			s[i] = reversebit(s[i])
		sum = 0
		for i in range(n):
			sum = sum + s[i] * 2 ** i
		return sum


class Solution:

	def toggleBits(self, N, L, R):
		one = 1
		bit_index = 1
		while bit_index != L:
			one <<= 1
			bit_index += 1
		while bit_index <= R:
			N ^= one
			one <<= 1
			bit_index += 1
		return N


class Solution:

	def toggle(self, n, p, b):
		mask = 1 << p
		return n & ~mask | b << p

	def getBit(self, n, i):
		return 0 if n & 1 << i > 0 else 1

	def toggleBits(self, N, L, R):
		for c in range(L - 1, R):
			b = self.getBit(N, c)
			N = self.toggle(N, c, b)
		return N


class Solution:

	def toggleBits(self, N, L, R):
		a = bin(N)[2:]
		lst = []
		for i in a:
			lst.append(i)
		l = len(lst) - L
		r = len(lst) - R
		for i in range(r, l + 1):
			if lst[i] == '0':
				lst[i] = '1'
			else:
				lst[i] = '0'
		result = int(''.join(lst), 2)
		return result


class Solution:

	def toggleBits(self, N, L, R):
		mask = ~0 << L - 1 ^ ~0 << R
		result = N ^ mask
		return result


class Solution:

	def toggleBits(self, N, L, R):
		n = list(bin(N).replace('0b', ''))
		for i in range(len(n) - L, len(n) - (R + 1), -1):
			if n[i] == '0':
				n[i] = '1'
			else:
				n[i] = '0'
		return int(''.join(n), 2)


class Solution:

	def toggleBits(self, N, L, R):
		n = bin(N).replace('0b', ' ')
		p = ''
		for i in range(len(n)):
			p += n[len(n) - i - 1]
		list_a = list(p)
		for j in range(L, R + 1):
			if list_a[j - 1] == '0':
				list_a[j - 1] = '1'
			else:
				list_a[j - 1] = '0'
		k = ''
		for m in range(len(list_a) - 1, -1, -1):
			k += list_a[m]
		n = int(k, 2)
		return n


class Solution:

	def toggleBits(self, N, L, R):
		li = list(bin(N)[2:])
		k = len(li)
		for i in range(k - L, k - R - 1, -1):
			if li[i] == '0':
				li[i] = '1'
			else:
				li[i] = '0'
		s = ''.join(li)
		return int(s, 2)


class Solution:

	def toggleBits(self, N, L, R):
		a = str(bin(N))[2:]
		b = list(map(str, a))
		for i in range(L, R + 1):
			if b[-i] == '1':
				b[-i] = '0'
			else:
				b[-i] = '1'
		g = ''.join(b)
		return int(g, 2)


class Solution:

	def toggleBits(self, N, L, R):
		a = 2 ** (L - 1) - 1
		b = 2 ** R - 1
		b -= a
		return N ^ b


import math

class Solution:

	def toggleBits(self, N, L, R):
		x = int(math.log2(N)) + 1
		L = x - L
		R = x - R
		for i in range(R, L + 1):
			N ^= 1 << x - (i + 1)
		return N


class Solution:

	def toggleBits(self, N, L, R):
		ans = 0
		for i in range(32):
			if L - 1 <= i <= R - 1:
				if (N >> i) % 2 == 0:
					ans += 1 << i
			elif (N >> i) % 2 != 0:
				ans += 1 << i
		return ans


class Solution:

	def toggleBits(self, N, L, R):
		x = bin(N)[2:][::-1]
		empty = ''
		for i in range(len(x)):
			if i in range(L - 1, R):
				empty += '1' if x[i] == '0' else '0'
			else:
				empty += x[i]
		return int(empty[::-1], 2)


class Solution:

	def toggleBits(self, N, L, R):
		mask = (1 << R - L + 1) - 1 << L - 1
		return N ^ mask


class Solution:

	def toggleBits(self, N, L, R):
		bitN = list(bin(N))
		toggleMap = {'0': '1', '1': '0'}
		for i in range(L, R + 1):
			bitN[-i] = toggleMap[bitN[-i]]
		return int(''.join(bitN), 2)


class Solution:

	def toggleBits(self, n, l, r):
		num = (1 << r) - 1 ^ (1 << l - 1) - 1
		return n ^ num


class Solution:

	def toggleBits(self, N, L, R):
		x = bin(N).replace('0b', '')
		x = list(x)
		for i in range(-L, -R - 1, -1):
			if x[i] == '0':
				x[i] = 1
				continue
			x[i] = 0
		(s, d) = (0, 0)
		for i in x[::-1]:
			s += 2 ** d * int(i)
			d += 1
		return s


class Solution:

	def toggleBits(self, N, L, R):
		b = bin(N)[2:]
		B = list(b)
		for i in range(-L, -R - 1, -1):
			if B[i] == '0':
				B[i] = '1'
			else:
				B[i] = 0
		(s, d) = (0, 0)
		for i in B[::-1]:
			s += 2 ** d * int(i)
			d = d + 1
		return s


class Solution:

	def toggleBits(self, N, L, R):
		while L <= R:
			N = N ^ 1 << L - 1
			L += 1
		return N


class Solution:

	def toggleBits(self, N, L, R):
		arr = []
		while N != 0:
			arr.append(N % 2)
			N = int(N / 2)
		for i in range(len(arr)):
			if i >= L - 1 and i <= R - 1:
				arr[i] = arr[i] ^ 1
			i += 1
		j = 1
		M = 0
		for i in range(len(arr)):
			if arr[i] == 1:
				M += j
			j = j * 2
		return M


class Solution:

	def toggleBits(self, n, start, end):
		a = bin(n)[2:]
		lst = []
		for i in a:
			lst.append(i)
		l = len(lst) - start
		r = len(lst) - end
		for i in range(r, l + 1):
			if lst[i] == '0':
				lst[i] = '1'
			elif lst[i] == '1':
				lst[i] = '0'
		result = int(''.join(lst), 2)
		return result


def btd(n):
	decimal = 0
	power = 1
	while n > 0:
		rem = n % 10
		n = n // 10
		decimal += rem * power
		power = power * 2
	return decimal

class Solution:

	def toggleBits(self, n, l, r):
		lst = list(format(n, 'b'))
		for i in range(len(lst) - l, len(lst) - r - 1, -1):
			if lst[i] == '1':
				lst[i] = '0'
			else:
				lst[i] = '1'
		strg = ''.join(lst)
		return btd(int(strg))


class Solution:

	def toggleBits(self, N, L, R):
		s = (1 << L - 1) - 1
		q = (1 << R) - 1
		temp = s ^ q
		return N ^ temp


class Solution:

	def toggleBits(self, N, L, R):
		b = bin(N)
		l = list(b)
		for i in range(L, R + 1):
			if l[-i] == '0':
				l[-i] = '1'
			else:
				l[-i] = '0'
		k = ''.join(l)
		m = int(k[2:])
		(d, p) = (0, 1)
		while m > 0:
			r = m % 10
			d = d + r * p
			p = p * 2
			m = m // 10
		return d


class Solution:

	def toggleBits(self, N, L, R):
		num = bin(N)[2:]
		num = num[::-1]
		output = num[:L - 1]
		for i in range(L, R + 1):
			if num[i - 1] == '1':
				output += '0'
			else:
				output += '1'
		output += num[R:]
		return int(output[::-1], 2)


class Solution:

	def toggleBits(self, N, l, r):
		N ^= (1 << r) - (1 << l - 1)
		return N


class Solution:

	def toggleBits(self, N, L, R):
		t = 1 << L - 1
		for i in range(L, R + 1):
			if t & N == 0:
				N += t
			else:
				N -= t
			t = t << 1
		return N


class Solution:

	def toggleBits(self, N, L, R):
		n = 1 << R
		n = n - 1
		n2 = 1 << L - 1
		n2 -= 1
		n = n ^ n2
		return N ^ n


import math

class Solution:

	def toggleBits(self, N, L, R):
		a = list(bin(N))
		b = a[::-1]
		for k in range(L - 1, R):
			if b[k] == '1':
				b[k] = '0'
			else:
				b[k] = '1'
		sum = 0
		for k in range(len(b) - 2):
			sum += int(b[k]) * pow(2, k)
		return sum


class Solution:

	def toggleBits(self, N, L, R):
		mask = 0
		for i in range(L - 1, R):
			mask += pow(2, i)
		return N ^ mask


class Solution:

	def toggleBits(self, N, L, R):
		bin = format(N, 'b')[::-1]
		bin = bin[:L - 1] + bin[L - 1:R].translate(str.maketrans('10', '01')) + bin[R:]
		return int(bin[::-1], 2)


class Solution:

	def toggleBits(self, N, L, R):
		a = 1 << L - 1
		b = 0
		for _ in range(L, R + 1):
			b += a
			a <<= 1
		return N ^ b


class Solution:

	def toggleBits(self, N, L, R):
		if N == 1:
			return 0
		s = bin(N)[2:]
		s = s[::-1]
		r = ''
		for i in range(len(s)):
			if L <= i + 1 <= R:
				y = int(s[i])
				if y == 1:
					r = r + '0'
				else:
					r = r + '1'
			else:
				r = r + s[i]
		r = r[::-1]
		c = int(r, 2)
		return c