Given a number N, swap the two nibbles in it and find the resulting number. 
 
Example 1:
Input:
N = 100
Output:
70
Explanation:
100 in binary is 01100100, 
two nibbles are (0110) and (0100)
If we swap the two nibbles, we get
01000110 which is 70 in decimal
Example 2:
Input:
N = 129
Output:
24
Explanation:
129 in binary is 10000001, 
two nibbles are (1000) and (0001)
If we swap the two nibbles, we get
00011000 which is 24 in decimal
Your Task:
You don't need to read input or print anything. Your task is to complete the function swapNibbles() which takes an integer N as input parameters and returns an integer after swapping nibbles in the binary representation of N.
 
Expected Time Complexity: O(1)
Expected Space Complexity: O(1)
 
Constraints:
0 ≤ N ≤ 255

Source: geeksforgeeks | Difficulty: EASY
Tags: Bit manipulation

Data structures

Starter code: #User function Template for python3

class Solution:

    def swapNibbles (self, N):

        # code here 

Solutions:
class Solution:

	def swapNibbles(self, N):
		d = format(N, '08b')
		r = d[4:8] + d[0:4]
		return int(r, 2)


class Solution:

	def swapNibbles(self, N):
		start = 15
		end = 15 << 4
		start &= N
		end &= N
		return start << 4 | end >> 4


class Solution:

	def swapNibbles(self, N):
		return (N & 15) << 4 | (N & 240) >> 4


class Solution:

	def swapNibbles(self, N):
		s = ''
		while N > 0:
			rem = N % 2
			s = str(rem) + s
			N = N // 2
		k = 8 - len(s)
		if k > 0:
			s = '0' * k + s
		s1 = s[:len(s) // 2]
		s2 = s[len(s) // 2:]
		s = s2 + s1
		s = s[::-1]
		val = 0
		for i in range(len(s)):
			val += 2 ** i * int(s[i])
		return val


class Solution:

	def swapNibbles(self, N):
		s = bin(N).replace('0b', '')
		a = 8 - len(s)
		if a:
			s = a * '0' + s
		s1 = s[0:4]
		s2 = s[4:]
		t = s2 + s1
		return int(t, 2)


class Solution:

	def swapNibbles(self, N):
		a = N % 16
		b = N // 16
		return a * 16 + b


class Solution:

	def swapNibbles(self, N):
		msk = (240 & N) >> 4
		msk2 = (15 & N) << 4
		return msk | msk2


class Solution:

	def swapNibbles(self, N):
		res = ''
		a = format(N, '08b')
		res = res + a[len(a) // 2:]
		res = res + a[:len(a) // 2]
		ans = int(res, 2)
		return ans


class Solution:

	def swapNibbles(self, N):
		bt = bin(N)[2:]
		ls = 8 * ['0']
		for i in range(0, len(bt)):
			ls[8 - len(bt) + i] = bt[i]
		ls = ls[4:] + ls[0:4]
		st = ''
		for e in ls:
			st = st + e
		N = int(st, 2)
		return N


class Solution:

	def swapNibbles(self, N):
		b = bin(N)[2:]
		for i in range(8 - len(b)):
			b = '0' + b
		f = b[:4]
		l = b[4:]
		b = l + f
		return int(b, 2)


class Solution:

	def swapNibbles(self, N):
		N = bin(N)[2:]
		N = '0' * (8 - len(N)) + N
		s1 = N[:4]
		s2 = N[4:]
		return int(s2 + s1, 2)


class Solution:

	def swapNibbles(self, N):
		b = bin(N)
		b = b.replace('0b', '0' * (10 - len(b)))
		b = b[4:] + b[:4]
		return int(b, 2)


class Solution:

	def swapNibbles(self, N):
		sum2 = 0
		N_bin = bin(N)
		N_bin = N_bin[2:]
		if N == 0:
			return 0
		if N == 1:
			return 16
		if N <= 15:
			return N * 16
		else:
			count2 = 7
			for i in N_bin[len(N_bin) - 4:]:
				if i == '1':
					sum2 += 2 ** count2
				count2 -= 1
			count3 = 0
			for i in N_bin[len(N_bin) - 5::-1]:
				if i == '1':
					sum2 += 2 ** count3
				count3 += 1
			return sum2


class Solution:

	def swapNibbles(self, N):
		s = bin(N)[2:]
		l = len(s)
		s = '0' * (8 - l) + s
		a = s[:4]
		b = s[4:]
		return eval('0b' + b + a)


class Solution:

	def swapNibbles(self, N):
		left = N & 15
		left = left << 4
		right = N >> 4
		return left | right


class Solution:

	def swapNibbles(self, N):
		x = bin(N).replace('0b', '')
		x = list(x)
		if len(x) < 8:
			zero = 8 - len(x)
			for i in range(zero):
				x.insert(0, '0')
		(x[4:], x[:4]) = (x[:4], x[4:])
		arr = ''
		for i in x:
			arr += i
		return int(arr, 2)


class Solution:

	def swapNibbles(self, N):
		a = N >> 4 & (1 << 4) - 1
		b = (N & (1 << 4) - 1) << 4
		return a + b


class Solution:

	def swapNibbles(self, N):
		x = bin(N)[2:].zfill(8)
		x1 = x[:4]
		x2 = x[4:]
		x = x2 + x1
		x = int(x, 2)
		return x


class Solution:

	def swapNibbles(self, n):
		x = bin(n).replace('0b', '')
		x = list(x)
		if len(x) < 8:
			q = 8 - len(x)
			for i in range(q):
				x.insert(0, '0')
		(x[4:], x[:4]) = (x[:4], x[4:])
		a = ''
		for i in x:
			a += i
		return int(a, 2)


class Solution:

	def swapNibbles(self, N):
		l = []
		while N != 0:
			if N % 2 == 0:
				l.append(0)
			else:
				l.append(1)
			N = N // 2
		l = l[::-1]
		while len(l) != 8:
			l.insert(0, 0)
		l = l[4:] + l[:4]
		count = 0
		n = 0
		for i in l[::-1]:
			count += 2 ** n * i
			n += 1
		return count


class Solution:

	def swapNibbles(self, N):
		k = bin(N)
		k = k[2:]
		while len(k) < 8:
			k = '0' + k
		l = k[:4]
		z = k[4:]
		s = z + l
		g = int(s, 2)
		return g


class Solution:

	def swapNibbles(self, N):
		a = bin(N)[2:]
		while len(a) != 8:
			a = '0' + a
		b = a[4:] + a[0:4]
		c = int(b, 2)
		return c


class Solution:

	def swapNibbles(self, N):
		s = N >> 4
		s = s & 255
		d = N << 4
		d = d & 255
		return s ^ d


class Solution:

	def swapNibbles(self, N):
		t1 = N >> 4
		r1 = t1 & 255
		t2 = N << 4
		r2 = t2 & 255
		return r1 ^ r2


class Solution:

	def swapNibbles(self, N):
		n = N
		t = n
		v = n << 4
		v = v & 255
		f = t >> 4
		return v | f


class Solution:

	def swapNibbles(self, N):
		bi = format(int(N), '08b')
		l = list(bi)
		l1 = l[0:len(l) // 2]
		l2 = l[len(l) // 2:len(l) + 1]
		z = l2 + l1
		s = ''.join(z)
		dec = int(s, 2)
		return dec


class Solution:

	def swapNibbles(self, N):
		bi = format(int(N), '08b')
		k = list(bi)
		l1 = k[len(k) // 2:len(k)]
		l2 = k[0:len(k) // 2]
		t = l1 + l2
		st = ''.join(t)
		dec = int(st, 2)
		return dec


class Solution:

	def swapNibbles(self, N):
		s = bin(N)[2:]
		while len(s) != 8:
			s = '0' + s
		s = s[4:] + s[:4]
		a = int(s, 2)
		return a


class Solution:

	def swapNibbles(self, N):
		l = [0 for i in range(8)]
		i = 0
		while N != 0:
			if N & 1:
				l[i] = 1
			N = N >> 1
			i += 1
		for i in range(4):
			temp = l[i]
			l[i] = l[-4 + i]
			l[-4 + i] = temp
		num = 0
		for i in range(8):
			num += l[i] * 2 ** i
		return num


class Solution:

	def swapNibbles(self, N):
		bin_str = bin(N)[2:]
		pad = '0' * (8 - len(bin_str))
		bin_str = pad + bin_str
		ans_str = bin_str[4:] + bin_str[:4]
		return int(ans_str, 2)


class Solution:

	def swapNibbles(self, N):
		num = bin(N)[2:]
		num = '0' * (8 - len(num)) + num
		return int(num[4:] + num[:4], 2)


class Solution:

	def swapNibbles(self, N):
		mask1 = 15
		mask2 = 15 * 16
		temp1 = N & mask1
		temp2 = N & mask2
		temp2 = temp2 >> 4
		return temp1 * 16 + temp2


class Solution:

	def swapNibbles(self, N):
		x = bin(N)[2:]
		x = [i for i in x]
		for i in range(8 - len(x)):
			x.insert(0, '0')
		a = x[:4]
		b = x[4:]
		s = ''
		for i in b:
			s += i
		for i in a:
			s += i
		return int(s, 2)


class Solution:

	def swapNibbles(self, N):
		return N % 16 << 4 | N // 16


class Solution:

	def swapNibbles(self, N):
		str1 = bin(N).replace('0b', '')
		if len(str1) < 8:
			n = len(str1)
			for i in range(0, 8 - n):
				str1 = '0' + str1
		stra = str1[0:4]
		strb = str1[4:8]
		str2 = strb + stra
		return int(str2, 2)


class Solution:

	def swapNibbles(self, N):
		l = N & 15
		l = l << 4
		r = N >> 4
		return l | r


class Solution:

	def swapNibbles(self, N):
		d = N & 15
		e = N & 240
		d = d << 4
		e = e >> 4
		res = d | e
		return res


class Solution:

	def convertToInt(self, s):
		num = 0
		n = len(s)
		for i in range(n):
			if s[n - i - 1] == '1':
				num += pow(2, i)
		return num

	def convertToBin(self, N):
		string = ''
		while N:
			rem = N % 2
			N = N // 2
			string = str(rem) + string
		while len(string) < 8:
			string = '0' + string
		return (string, len(string))

	def swapNibbles(self, N):
		(string, n) = self.convertToBin(N)
		num = self.convertToInt(string[n // 2:] + string[:n // 2])
		return num


class Solution:

	def swapNibbles(self, N):
		x = N >> 4
		y = N << 4
		return (x | y) & 255


class Solution:

	def swapNibbles(self, N):
		b = bin(N)[2:].zfill(8)
		b = int(b[4:] + b[:4])
		(dec, i, n) = (0, 0, 0)
		while b != 0:
			d = b % 10
			dec = dec + d * pow(2, i)
			b = b // 10
			i += 1
		return dec


class Solution:

	def swapNibbles(self, N):
		arr = [0] * 8
		num1 = 0
		for i in range(8):
			arr[7 - i] = N & 1
			N = N >> 1
		for i in range(4):
			num1 += arr[4 + i] * (1 << 7 - i) + arr[i] * (1 << 3 - i)
		return num1


class Solution:

	def swapNibbles(self, x):
		return (x & 15) << 4 | (x & 240) >> 4


class Solution:

	def swapNibbles(self, n):
		left = n & 15
		left = left << 4
		right = n >> 4
		return left | right


class Solution:

	def swapNibbles(self, N):
		h1 = N & 15
		h2 = N & 240
		return (h1 << 4) + (h2 >> 4)


class Solution:

	def swapNibbles(self, N):
		h1 = (N & 15) << 4
		h2 = (N & 255 - 15) >> 4
		return h1 + h2


class Solution:

	def swapNibbles(self, N):
		num = bin(N)
		temp = list(num[2:])
		bi = []
		if len(temp) < 8:
			for i in range(8 - len(temp)):
				bi.append('0')
			for i in range(len(temp)):
				bi.append(temp[i])
		else:
			bi = temp
		bi_new = bi[4:] + bi[:4]
		sum = 0
		for i in range(7, -1, -1):
			sum = sum + int(bi_new[i]) * 2 ** (7 - i)
		return sum


class Solution:

	def swapNibbles(self, N):
		a = bin(N)
		z = (8 - (len(a) - 2)) * '0'
		a = z + a[2:]
		a = '0b' + a[4:] + a[0:4]
		return int(a, 2)


class Solution:

	def swapNibbles(self, N):
		n1 = N & 240
		n1 = n1 >> 4
		n2 = N & 15
		n2 = n2 << 4
		return n2 | n1


class Solution:

	def swapNibbles(self, N):
		n = bin(N)[2:]
		while len(n) < 8:
			n = '0' + n
		n = n[4:] + n[:4]
		return eval('0b' + n)


class Solution:

	def swapNibbles(self, N):
		getbinary = lambda x, n: format(x, 'b').zfill(n)
		N = getbinary(N, 8)
		ans = [N[4:], N[0:4]]
		return int(''.join(ans), 2)


class Solution:

	def swapNibbles(self, N):
		s = str(bin(N))[2:]
		for itr in range(8 - len(s)):
			s = '0' + s
		res1 = s[:4]
		res2 = s[4:]
		return int(res2 + res1, 2)


class Solution:

	def swapNibbles(self, N):
		s = str(bin(N))[2:].zfill(8)
		newnum = s[4:8] + s[0:4]
		res = int(newnum, 2)
		return res
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		N = int(input())
		ob = Solution()
		print(ob.swapNibbles(N))


class Solution:

	def swapNibbles(self, N):
		b = bin(N)[2:]
		while len(b) != 8:
			b = '0' + b
		a = b[:len(b) // 2]
		a1 = b[len(b) // 2:]
		return int(a1 + a, 2)
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		N = int(input())
		ob = Solution()
		print(ob.swapNibbles(N))


class Solution:

	def swapNibbles(self, N):
		N = '{:08b}'.format(N)
		m = N[0:4]
		k = N[4:]
		N = k + m
		return int(N, 2)
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		N = int(input())
		ob = Solution()
		print(ob.swapNibbles(N))


class Solution:

	def swapNibbles(self, N):
		return (N & 15) << 4 | (N & 240) >> 4
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		N = int(input())
		ob = Solution()
		print(ob.swapNibbles(N))


class Solution:

	def swapNibbles(self, N):
		s = bin(N)
		s = s[2:]
		s = (8 - len(s)) * '0' + s
		s = s[4:] + s[:4]
		c = 7
		y = 0
		for i in s:
			if i == '1':
				y += 2 ** c
			c -= 1
		return y
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		N = int(input())
		ob = Solution()
		print(ob.swapNibbles(N))


class Solution:

	def swapNibbles(self, N):
		binary = str(bin(N))[2:]
		binary = (8 - len(binary)) * '0' + binary
		swapped = binary[4:] + binary[:4]
		return int(swapped, 2)


class Solution:

	def swapNibbles(self, N):
		t = bin(N)[2:].zfill(8)
		return int(t[4:] + t[:4], 2)


class Solution:

	def swapNibbles(self, N):
		N = bin(N)[2:]
		s = list(N)
		if len(s) < 8:
			c = 8 - len(s)
			for i in range(c):
				s.insert(0, '0')
		(s[:4], s[4:]) = (s[4:], s[:4])
		a = ''
		for i in s:
			a += i
		return int(a, 2)