Given a number X and two positions (from right side) in binary representation of X, write a program that swaps N bits at given two positions and returns the result.
 
Example 1:
Input:
X = 47
P1 = 1
P2 = 5
N = 3
Output: 227
Explanation:
The 3 bits starting from the second bit 
(from the right side) are swapped with 3 bits
starting from 6th position (from the right side) 
X = 47 (00101111)
[001]0[111]1
ANS = [111]0[001]1
ANS = 227 (11100011)
Hence, the result is 227.  
 
Example 2:
Input:
X = 28
P1 = 0
P2 = 3
N = 2
Output: 7
Your Task:  
You don't need to read input or print anything. Your task is to complete the function swapBits() which takes the integer X, integer P1, integer P2, and integer N as input parameters and returns the new integer after swapping. 
Expected Time Complexity: O(1)
Expected Auxiliary Space: O(1)
 
Constraints:
1 ≤ X ≤ 200
0 ≤ P1 < P2 ≤ 11
1 ≤ N ≤ 5

Source: geeksforgeeks | Difficulty: EASY
Tags: Bit manipulation

Data structures

Starter code: #User function Template for python3



class Solution:

    def swapBits(self, X, P1, P2, N):

        return 1





Solutions:
class Solution:

	def swapBits(self, X, P1, P2, N):
		mask = (1 << N) - 1
		a = (X & mask << P1) >> P1
		b = (X & mask << P2) >> P2
		X &= ~(mask << P1) & ~(mask << P2)
		X |= a << P2 | b << P1
		return X


class Solution:

	def swapBits(self, X, P1, P2, N):
		for i in range(0, N):
			temp = X >> i + P1 & 1 ^ X >> i + P2 & 1
			X ^= temp << i + P1
			X ^= temp << i + P2
		return X


class Solution:

	def swapBits(self, X, P1, P2, N):
		s1 = X >> P1 & (1 << N) - 1
		s2 = X >> P2 & (1 << N) - 1
		xor = s1 ^ s2
		xor = xor << P1 | xor << P2
		X ^= xor
		return X


class Solution:

	def swapBits(self, X, P1, P2, N):
		for n in range(N):
			if X >> P1 + n & 1 != X >> P2 + n & 1:
				bitmask = 1 << P1 + n | 1 << P2 + n
				X ^= bitmask
		return X


class Solution:

	def swapBits(self, X, P1, P2, N):
		num = bin(X)[2:]
		num = num[::-1] + 32 * '0'
		output = num[:P1] + num[P2:P2 + N] + num[P1 + N:P2] + num[P1:P1 + N] + num[P2 + N:]
		return int(output[::-1], 2)


class Solution:

	def swapBits(self, X, P1, P2, N):
		a = X >> P1 & (1 << N) - 1
		b = X >> P2 & (1 << N) - 1
		c = a ^ b
		d = c << P1 | c << P2
		e = d ^ X
		return e


class Solution:

	def swapBits(self, X, P1, P2, N):
		for _ in range(N):
			if (X & 1 << P1) >> P1 != (X & 1 << P2) >> P2:
				X ^= 1 << P1 | 1 << P2
			(P1, P2) = (P1 + 1, P2 + 1)
		return X


class Solution:

	def swapBits(self, x, p1, p2, n):
		set1 = x >> p1 & (1 << n) - 1
		set2 = x >> p2 & (1 << n) - 1
		xor = set1 ^ set2
		xor = xor << p1 | xor << p2
		result = x ^ xor
		return result


class Solution:

	def swapBits(self, x, p1, p2, n):
		b = bin(x)[2:]
		b = '0' * (2 * n) + b
		b = list(b)[::-1]
		s1 = b[p1:p1 + n]
		s2 = b[p2:p2 + n]
		b[p1:p1 + n] = s2
		b[p2:p2 + n] = s1
		b = ''.join(b)[::-1]
		s = int(b, 2)
		return s


class Solution:

	def swapBits(self, s, p1, p2, n):
		s3 = '0' * 2 * n
		s1 = bin(s)[2:]
		s1 = s1[::-1] + s3
		l = s1[p1:p1 + n]
		l1 = s1[p2:p2 + n]
		s2 = s1[0:p1] + l1 + s1[p1 + n:p2] + l + s1[p2 + n:]
		s2 = s2[::-1]
		return int(s2, 2)


class Solution:

	def swapBits(self, x, p1, p2, n):
		a = bin(x)
		a = a[2:]
		b = '0' * 2 * n + a
		l = list(b)
		l1 = list(b)
		l1 = l1[::-1]
		l = l[::-1]
		d = l[p1:p1 + n]
		d1 = l[p2:p2 + n]
		l1[p1:p1 + n] = d1
		l1[p2:p2 + n] = d
		l1 = l1[::-1]
		f = ''.join(l1)
		return int(f, 2)


import math

class Solution:

	def swapBits(self, X, P1, P2, N):
		bits = math.floor(math.log(X, 2)) + 1
		s = (1 << N) - 1
		r = X & s << P1
		l = X & s << P2
		total = X + (r << P2 - P1) - l + (l >> P2 - P1) - r
		return total


import math

class Solution:

	def swapBits(self, X, P1, P2, N):
		bits = math.floor(math.log(X, 2)) + 1
		set_bits = (1 << N) - 1
		right_set = X & set_bits << P1
		left_set = X & set_bits << P2
		total = X + (right_set << P2 - P1) - left_set + (left_set >> P2 - P1) - right_set
		return total


class Solution:

	def swapBits(self, X, P1, P2, N):
		set1 = X >> P1 & (1 << N) - 1
		set2 = X >> P2 & (1 << N) - 1
		R = set1 ^ set2
		R = R << P1 | R << P2
		res = R ^ X
		return res


class Solution:

	def swapBits(self, X, P1, P2, N):
		X = '{:016b}'.format(X)
		X = X[::-1]
		l = X[:P1]
		r = X[P1:P1 + N]
		k = X[P1 + N:P2]
		z = X[P2:P2 + N]
		m = X[P2 + N:]
		f = l + z + k + r + m
		f = f[::-1]
		return int(f, 2)
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		obj = Solution()
		(X, P1, P2, N) = map(int, input().split())
		print(obj.swapBits(X, P1, P2, N))


import math

class Solution:

	def swapBits(self, n, p1, p2, num):
		bitarray = [0] * 32
		ind = 31
		while n != 1:
			bitarray[ind] = n % 2
			ind -= 1
			n = n // 2
		bitarray[ind] = 1
		p3 = 31 - p1
		p4 = 31 - p2
		for i in range(1, num + 1):
			(bitarray[p3], bitarray[p4]) = (bitarray[p4], bitarray[p3])
			p3 -= 1
			p4 -= 1
		ans = 0
		for i in range(31, -1, -1):
			ans = ans + math.pow(2, 31 - i) * bitarray[i]
		return int(ans)
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		obj = Solution()
		(X, P1, P2, N) = map(int, input().split())
		print(obj.swapBits(X, P1, P2, N))


class Solution:

	def swapBits(self, X, P1, P2, N):
		a = X >> P1 & (1 << N) - 1
		b = X >> P2 & (1 << N) - 1
		c = a ^ b
		d = c << P1 | c << P2
		e = d ^ X
		return e
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		obj = Solution()
		(X, P1, P2, N) = map(int, input().split())
		print(obj.swapBits(X, P1, P2, N))


class Solution:

	def swapBits(self, X, P1, P2, N):
		Xor = (X >> P1 ^ X >> P2) & (1 << N) - 1
		return X ^ (Xor << P1 | Xor << P2)
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		obj = Solution()
		(X, P1, P2, N) = map(int, input().split())
		print(obj.swapBits(X, P1, P2, N))


class Solution:

	def swapBits(self, X, P1, P2, N):
		s = '1' * N
		bitmask = int(s, 2)
		xored = X >> P1 & bitmask ^ X >> P2 & bitmask
		X ^= xored << P1 | xored << P2
		return X


class Solution:

	def swapBits(self, x, p1, p2, n):
		h = bin(x)
		h = str(h)
		h = list(h)
		h = h[2:]
		h = h[::-1]
		h = h + ['0'] * (p2 + n)
		k1 = h[p1:p1 + n]
		k2 = h[p2:p2 + n]
		g1 = 0
		g2 = 0
		for i in range(len(h)):
			if i >= p1 and i < p1 + n:
				h[i] = k2[g1]
				g1 = g1 + 1
			elif i >= p2 and i < p2 + n:
				h[i] = k1[g2]
				g2 = g2 + 1
		h = h[::-1]
		h = ''.join(h)
		return int(h, 2)


class Solution:

	def swapBits(self, X, P1, P2, N):
		j = '{:016b}'.format(X)
		p = list(j)
		for i in range(N):
			(p[len(p) - 1 - P1 - i], p[len(p) - 1 - P2 - i]) = (p[len(p) - 1 - P2 - i], p[len(p) - 1 - P1 - i])
		res = ''
		res = res.join(p)
		return int(res, 2)


class Solution:

	def swapBits(self, X, P1, P2, N):
		e1 = self.extractBits(X, N, P1) << P2
		e2 = self.extractBits(X, N, P2) << P1
		rb = self.resetBits(X, N, P1, P2)
		return rb | e1 | e2

	def resetBits(self, number, numberOfBits, position1, position2):
		mask = (1 << numberOfBits) - 1
		number = number & ~(mask << position1)
		number = number & ~(mask << position2)
		return number

	def extractBits(self, number, numberOfBits, position):
		mask = (1 << numberOfBits) - 1
		return (number & mask << position) >> position


import math

class Solution:

	def swapBits(self, X, P1, P2, N):
		set_bits = (1 << N) - 1
		right_set = X >> P1 & set_bits
		left_set = X >> P2 & set_bits
		xor = right_set ^ left_set
		X ^= xor << P2
		X ^= xor << P1
		return X


class Solution:

	def swapBits(self, x, p1, p2, n):
		n_bits_from_p1 = x >> p1 & (1 << n) - 1
		n_bits_from_p2 = x >> p2 & (1 << n) - 1
		mask = n_bits_from_p1 ^ n_bits_from_p2
		swapper_mask = mask << p1 | mask << p2
		return swapper_mask ^ x


class Solution:

	def swapBits(self, X, P1, P2, N):
		set1 = X >> P1 & (1 << N) - 1
		set2 = X >> P2 & (1 << N) - 1
		xor = set1 ^ set2
		xor = xor << P1 | xor << P2
		result = X ^ xor
		return result


class Solution:

	def swapBits(self, X, P1, P2, N):
		mask = (1 << N) - 1
		mask1 = mask << P1
		mask2 = mask << P2
		if P1 < P2:
			bits1 = (X & mask1) << P2 - P1
			bits2 = (X & mask2) >> P2 - P1
		else:
			bits1 = (X & mask1) >> P1 - P2
			bits2 = (X & mask2) << P1 - P2
		rem_bits = X & ~(mask1 | mask2)
		return bits1 | bits2 | rem_bits


class Solution:

	def swapBits(self, X, P1, P2, N):
		m = 1 << N - 1 | (1 << N - 1) - 1
		(bs1, bs2) = (m & X >> P1, m & X >> P2)
		X &= ~(m << P1 | m << P2)
		X = X | bs1 << P2 | bs2 << P1
		return X


class Solution:

	def swapBits(self, X, P1, P2, N):
		b = bin(X)[2:]
		b = '0' * (16 - len(b)) + b
		l = []
		for i in b:
			l.append(i)
		n = len(b)
		P1 = n - P1 - 1
		P2 = n - P2 - 1
		for i in range(N):
			(l[P1], l[P2]) = (l[P2], l[P1])
			P1 -= 1
			P2 -= 1
		ans = ''.join(l)
		return int(ans, 2)


class Solution:

	def swapBits(self, X, p1, p2, n):
		x = bin(X).replace('0b', '')
		ln = len(x)
		r_l = '0' * (16 - ln) + x
		r = []
		for i in r_l:
			r.append(i)
		r.reverse()
		t1 = ''
		t2 = ''
		for i in range(len(r)):
			if i == p1:
				for j in range(n):
					t1 += r[i]
					i += 1
			if i == p2:
				for k in range(n):
					t2 += r[i]
					i += 1
		for i in range(len(r)):
			if i == p1:
				for j in range(n):
					r[i] = t2[j]
					i += 1
			elif i == p2:
				for k in range(n):
					r[i] = t1[k]
					i += 1
		sum = 0
		x = 0
		for num in r:
			sum += 2 ** x * int(num)
			x += 1
		return sum


class Solution:

	def swapBits(self, X, P1, P2, N):
		r1 = bin(X).lstrip('0b')
		while True:
			if len(r1) >= 16:
				break
			else:
				r1 = '0' + r1
		r = []
		for i in str(r1):
			r.append(i)
		r.reverse()
		t = ''
		t1 = ''
		y = ''
		for i in range(len(r)):
			if i == P1:
				for j in range(N):
					t += r[i]
					i = i + 1
			if i == P2:
				for k in range(N):
					t1 += r[i]
					i = i + 1
		for i in range(len(r)):
			if i == P1:
				for j in range(N):
					r[i] = t1[j]
					i = i + 1
			if i == P2:
				for k in range(N):
					r[i] = t[k]
					i = i + 1
		r.reverse()
		for i in r:
			y += i
		l = int(y, 2)
		return l


class Solution:

	def swapBits(self, X, P1, P2, N):
		b = bin(X)[2:]
		b = '0' * (32 - len(b)) + b
		lis = []
		for i in b:
			lis.append(i)
		n = len(b)
		P1 = n - P1 - 1
		P2 = n - P2 - 1
		for j in range(N):
			(lis[P1], lis[P2]) = (lis[P2], lis[P1])
			P1 -= 1
			P2 -= 1
		ans = ''.join(lis)
		return int(ans, 2)


class Solution:

	def swapBits(self, X, P1, P2, N):
		maskP1 = X >> P1 & (1 << N) - 1
		maskP2 = X >> P2 & (1 << N) - 1
		mask3 = maskP1 ^ maskP2
		mask3 = mask3 << P1 | mask3 << P2
		Xnew = X ^ mask3
		return Xnew


class Solution:

	def swapBits(self, x, p1, p2, n):
		mask0 = 255 >> 8 - n
		mask1 = mask0 << p1
		mask2 = mask0 << p2
		lift1 = x & mask1
		lift2 = x & mask2
		move1 = lift1 << p2 - p1
		move2 = lift2 >> p2 - p1
		return x ^ lift1 ^ lift2 | move1 | move2


class Solution:

	def swapBits(self, X, P1, P2, N):
		a = bin(X)[2:]
		k = P2 + N
		if len(a) < k:
			diff = k - len(a)
			a = '0' * diff + a
		n = len(a)
		ch1 = a[n - P1 - N:n - P1]
		ch2 = a[n - P2 - N:n - P2]
		out = a[:n - P2 - N] + ch1 + a[n - P2:n - P1 - N] + ch2 + a[n - P1:]
		return int(out, 2)


class Solution:

	def swapBits(self, X, P1, P2, N):
		b = bin(X)[2:]
		b = '0' * (2 * N + P1 + P2 - len(b)) + b
		if P1 == 0:
			s1 = ''
			s2 = b[-P1 - N:]
		else:
			s1 = b[-P1:]
			s2 = b[-P1 - N:-P1]
		s3 = b[-P2:-P1 - N]
		s4 = b[-P2 - N:-P2]
		s5 = b[:-P2 - N]
		return int(s5 + s2 + s3 + s4 + s1, 2)


class Solution:

	def swapBits(self, X, p1, p2, n):
		s = bin(X).replace('0b', '')
		s = '0' * 20 + s
		s = list(s)
		s.reverse()
		ans = s.copy()
		x = p2
		for i in range(p1, p1 + n):
			ans[i] = s[x]
			x += 1
		x = p1
		for i in range(p2, p2 + n):
			ans[i] = s[x]
			x += 1
		ans.reverse()
		ans = ''.join(ans)
		res = int(ans, 2)
		return res


class Solution:

	def swapBits(self, X, P1, P2, N):
		for i in range(0, N):
			temp1 = 1 << P1 + i
			temp2 = 1 << P2 + i
			if temp1 & X == temp2 & X or (temp1 & X == temp1 and temp2 & X == temp2):
				continue
			else:
				X ^= temp2
				X ^= temp1
		return X


class Solution:

	def swapBits(self, X, P1, P2, N):
		mask0 = 255 >> 8 - N
		mask1 = mask0 << P1
		mask2 = mask0 << P2
		lift1 = X & mask1
		lift2 = X & mask2
		move1 = lift1 << P2 - P1
		move2 = lift2 >> P2 - P1
		return X ^ lift1 ^ lift2 | move1 | move2


class Solution:

	def swapBits(self, X, P1, P2, N):
		bx = bin(X)[2:].zfill(16)
		k = len(bx) - 1
		f1 = bx[k - P1 - N + 1:k - P1 + 1]
		f2 = bx[k - P2 - N + 1:k - P2 + 1]
		bx = bx[:k - P1 - N + 1] + f2 + bx[k - P1 + 1:]
		bx = bx[:k - P2 - N + 1] + f1 + bx[k - P2 + 1:]
		return int(bx, 2)


class Solution:

	def swapBits(self, X, P1, P2, N):
		l = bin(X)[2:].zfill(16)
		ln = len(l)
		Y = l[:ln - P2 - N] + l[ln - P1 - N:ln - P1] + l[ln - P2:ln - P1 - N] + l[ln - P2 - N:ln - P2] + l[ln - P1:]
		return int(Y, 2)


class Solution:

	def swapBits(self, X, P1, P2, N):
		mask = (1 << N) - 1
		a = X >> P1 & mask
		b = X >> P2 & mask
		c = a ^ b
		XOR = c << P1 | c << P2
		ans = XOR ^ X
		return ans


class Solution:

	def swapBits(self, x, p1, p2, n):
		ans1 = x >> p1 & (1 << n) - 1
		ans2 = x >> p2 & (1 << n) - 1
		xor = ans1 ^ ans2
		xor = xor << p1 | xor << p2
		ans = x ^ xor
		return ans


class Solution:

	def swapBits(self, X, P1, P2, N):
		(x, p1, p2, n) = (X, P1, P2, N)
		set1 = x >> p1 & (1 << n) - 1
		set2 = x >> p2 & (1 << n) - 1
		xor = set1 ^ set2
		xor = xor << p1 | xor << p2
		result = x ^ xor
		return result


class Solution:

	def swapBits(self, X, p1, p2, N):
		x = bin(X)
		x = x[2:]
		n = len(x)
		x = list(x)
		x = x[::-1]
		if p2 + N > n:
			while n < p2 + N:
				x.append(0)
				n += 1
		(x[p1:p1 + N], x[p2:p2 + N]) = (x[p2:p2 + N], x[p1:p1 + N])
		c = 0
		for i in range(n):
			if x[i] == '1':
				c += 2 ** i
		return c


class Solution:

	def swapBits(self, X, P1, P2, N):
		arr = []
		for i in range(64):
			arr.append(0)
		j = 0
		while X:
			last_bit = X & 1
			X = X >> 1
			arr[j] = last_bit
			j += 1
		i = 0
		while i < N:
			temp = arr[P1 + i]
			arr[P1 + i] = arr[P2 + i]
			arr[P2 + i] = temp
			i += 1
		i = len(arr) - 1
		ans = 0
		while i >= 0:
			ans = ans * 2 + arr[i]
			i -= 1
		return ans


class Solution:

	def swapBits(self, X, P1, P2, N):
		binary = [i for i in bin(X)[2:]]
		extra = 16 - len(binary)
		for i in range(extra):
			binary.insert(0, '0')
		l = len(binary)
		for i in range(N):
			cb = l - P1 - i - 1
			nb = l - P2 - i - 1
			(binary[cb], binary[nb]) = (binary[nb], binary[cb])
		ans = ''.join(binary)
		return int(ans, 2)


class Solution:

	def swapBits(self, s, p1, p2, n):
		s1 = s
		for i in range(n):
			if s >> p1 + i & 1 == 1 and s >> p2 + i & 1 == 0:
				s1 = s1 - (1 << p1 + i) + (1 << p2 + i)
			elif s >> p1 + i & 1 == 0 and s >> p2 + i & 1 == 1:
				s1 = s1 - (1 << p2 + i) + (1 << p1 + i)
		return s1


class Solution:

	def swapBits(self, X, P1, P2, N):
		binnum = bin(X).replace('0b', '')
		if len(binnum) < N + P2:
			binnum = (binnum[::-1] + '0' * (N + P2 - len(binnum)))[::-1]
		binnum = binnum[::-1]
		binnum = binnum[0:P1] + binnum[P2:P2 + N] + binnum[P1 + N:P2] + binnum[P1:P1 + N] + binnum[P2 + N:]
		binnum = binnum[::-1]
		return int(binnum, 2)


class Solution:

	def swapBits(self, X, P1, P2, N):
		Xb = list(bin(X)[2:])
		Xb = ['0'] * (P2 + N - len(Xb)) + Xb
		Xb.reverse()
		for i in range(N):
			(Xb[P1 + i], Xb[P2 + i]) = (Xb[P2 + i], Xb[P1 + i])
		Xb.reverse()
		return int(''.join(Xb), 2)


class Solution:

	def swapBits(self, X, P1, P2, N):
		cal = max(P1, P2) + N
		b = bin(X)[2:].zfill(cal)
		arr = list(b)
		i = len(b) - P1 - 1
		j = len(b) - P2 - 1
		while N:
			N -= 1
			(arr[i], arr[j]) = (arr[j], arr[i])
			i -= 1
			j -= 1
		return int(''.join(arr), 2)


class Solution:

	def swapBits(self, X, P1, P2, N):
		X = bin(X)[2:]
		l = len(X)
		add = P2 + N + 2 - l
		X = add * '0' + X
		l = len(X)
		first = X[l - P1 - N:l - P1]
		second = X[l - P2 - N:l - P2]
		X = X[:l - P2 - N] + first + X[l - P2:l - P1 - N] + second + X[l - P1:]
		return int(X, 2)


class Solution:

	def swapBits(self, x, p1, p2, n):
		m = 1 << n - 1 | (1 << n - 1) - 1
		(bs1, bs2) = (m & x >> p1, m & x >> p2)
		x &= ~(m << p1 | m << p2)
		x = x | bs1 << p2 | bs2 << p1
		return x


class Solution:

	def swapBits(self, X, P1, P2, N):
		j = bin(X)[2:].zfill(32)
		swap1 = j[-(P1 + 1):-(P1 + 1 + N):-1]
		swap2 = j[-(P2 + 1):-(P2 + 1 + N):-1]
		s1 = swap1[::-1]
		s2 = swap2[::-1]
		result = eval('0b' + j[0:len(j) - P2 - N] + s1 + j[len(j) - P2:len(j) - P1 - N] + s2 + j[len(j) - P1:])
		return result


class Solution:

	def swapBits(self, X, P1, P2, N):
		l = list(format(X, '016b'))
		if P1 > 0:
			(l[-(P1 + N):-P1], l[-(P2 + N):-P2]) = (l[-(P2 + N):-P2], l[-(P1 + N):-P1])
		else:
			(l[-(P1 + N):], l[-(P2 + N):-P2]) = (l[-(P2 + N):-P2], l[-(P1 + N):])
		x = ''.join(l)
		return int(x, 2)


class Solution:

	def swapBits(self, X, P1, P2, n):
		a = X >> P1 & (1 << n) - 1
		b = X >> P2 & (1 << n) - 1
		c = a ^ b
		c = c << P1 | c << P2
		ans = X ^ c
		return ans


class Solution:

	def swapBits(self, X, a, b, n):
		m = 1 << N - 1
		m = m | m - 1
		bs1 = m & X >> P1
		bs2 = m & X >> P2
		X &= ~(m << P1)
		X &= ~(m << P2)
		X |= bs1 << P2
		X |= bs2 << P1
		return X


class Solution:

	def swapBits(self, x, p1, p2, n):
		for i in range(n):
			a = x >> i + p1 & 1
			b = x >> i + p2 & 1
			t = a ^ b
			x ^= t << i + p1
			x ^= t << i + p2
		return x


class Solution:

	def swapBits(self, X, P1, P2, N):
		n = 1
		for i in range(N - 1):
			n = n << 1
			n += 1
		part1 = X >> P1 & n
		part2 = X >> P2 & n
		X = X - (part1 << P1) - (part2 << P2) + (part2 << P1) + (part1 << P2)
		return X


class Solution:

	def swapBits(self, X, P1, P2, N):
		binary = [0] * 16
		i = 15
		while X:
			binary[i] = X % 2
			i -= 1
			X //= 2
		i = 0
		while N:
			temp = binary[15 - P1 - i]
			binary[15 - P1 - i] = binary[15 - P2 - i]
			binary[15 - P2 - i] = temp
			N -= 1
			i += 1
		i = 0
		string = ''
		while binary[i] != 1:
			i += 1
		for j in range(i, 16):
			string += str(binary[j])
		return int(string, 2)


class Solution:

	def swapBits(self, X, P1, P2, N):
		a = 0
		b = 0
		exponents2 = [1, 2, 4, 8, 16, 32, 64, 128]
		i = 0
		xCopy = X
		while i < P2 + N:
			if P1 <= i < P1 + N:
				if xCopy & 1:
					a += exponents2[i]
			elif P2 <= i < P2 + N:
				if xCopy & 1:
					b += exponents2[i]
			i += 1
			xCopy = xCopy // 2
		ans = X - a + a * exponents2[P2 - P1] - b + b // exponents2[P2 - P1]
		return ans


class Solution:

	def swapBits(self, x, p1, p2, n):
		binary = bin(x)[2:]
		binary = '0' * (8 - len(binary)) + binary
		reverse = binary[::-1]
		res = ''
		if n + p2 > n:
			reverse = reverse + '0' * p2
		s1 = reverse[p1:n + p1]
		s1 = s1 + '0' * (n - len(s1))
		s2 = reverse[p2:n + p2]
		s2 = s2 + '0' * (n - len(s2))
		res = reverse[0:p1] + s2 + reverse[n + p1:p2] + s1 + reverse[n + p2:]
		return int(res[::-1], 2)


class Solution:

	def swapBits(self, val, P1, P2, N):
		result = ''
		while val > 0:
			result += str(val % 2)
			val = int(val / 2)
		result = list(result[len(result)::-1])
		totalL = P2 + N
		while len(result) < totalL:
			result.insert(0, '0')
		result.reverse()
		(result[P1:P1 + N], result[P2:P2 + N]) = (result[P2:P2 + N], result[P1:P1 + N])
		result.reverse()
		return int(''.join(result), 2)