Given an integer N and an integer D, rotate the binary representation of the integer N by D digits to the left as well as right and print the results in decimal values after each of the rotation.
Note: Integer N is stored using 16 bits. i.e. 12 will be stored as 0000.....001100.
Example 1:
Input:
N = 28, D = 2
Output:
112
7
Explanation: 28 in Binary is:
000...011100
Rotating left by 2 positions, it becomes
00...1110000 = 112 (in decimal).
Rotating right by 2 positions, it becomes
000...000111 = 7 (in decimal).
Ã¢â¬â¹Example 2:
Input: 
N = 29, D = 2
Output: 
116
16391
Explanation: 29 in Binary is:
000...011101
Rotating left by 2 positions, it becomes
00...1110100 = 116 (in decimal).
Rotating right by 2 positions, it becomes
010...000111 = 16391 (in decimal).
Your Task:
You don't need to read input or print anything. Your task is to complete the function rotate() which takes the integer N and integer D as inputs and returns an array of size 2 where arr[0] = Decimal value after left rotation and arr[1] = Decimal value after right rotation.
Expected Time Complexity: O(1).
Expected Auxiliary Space: O(1).
Constraints:
1 <= N <  2^{16}
1 <= D <= 10^{5}

Difficulty: EASY
Tags: String algorithms

Bit manipulation

Data structures
Skill types: Bit manipulation

Data structures
Expected Time Complexity: O(1).
Expected Auxiliary Space: O(1).

Starter code:
#User function Template for python3

class Solution:
    def rotate(self, N, D):
        # code here

Solutions:
class Solution:

	def rotate(self, n, d):
		d = d % 16
		ls = n << d
		rs = n >> 16 - d
		a = (rs | ls) & (1 << 16) - 1
		rs = n >> d
		ls = n << 16 - d
		b = (rs | ls) & (1 << 16) - 1
		return [a, b]


class Solution:
	ans = []

	def rotate(self, N, D):
		self.N = N
		self.D = D
		lista_N = list(bin(N)[2:])
		for i in range(16 - len(lista_N)):
			lista_N.insert(0, '0')
		if D >= 16:
			D = D % 16
		lista_NL = lista_N[D:] + lista_N[:D]
		lista_NR = lista_N[-D:] + lista_N[:-D]
		NL = ''.join(lista_NL)
		NR = ''.join(lista_NR)
		NL = int(NL, 2)
		NR = int(NR, 2)
		return (NL, NR)


class Solution:

	def rotate(self, N, D):
		arr = [0] * 2
		int_bits = 16
		if D > 16:
			D = D % 16
		arr[0] = (N << D | N >> int_bits - D) & 65535
		arr[1] = (N >> D | N << int_bits - D) & 65535
		return arr


class Solution:

	def rotate(self, n, d):
		b = 16
		if d > b:
			d %= b
		ans = []
		ans.append((n << d | n >> b - d) & 65535)
		ans.append((n >> d | n << b - d) & 65535)
		return ans


class Solution:

	def rotate(self, N, D):
		m = N
		m1 = N
		for i in range(D):
			if m & 1 == 1:
				m = m >> 1
				l = 1 << 15
				m = m | l
			else:
				m = m >> 1
		d = D % 16
		m1 = (m1 << d | m1 >> 16 - d) & 65535
		return [m1, m]


class Solution:

	def rotate(self, N, D):
		D %= 16
		return [(N << D | N >> 16 - D) & 65535, (N >> D | N << 16 - D) & 65535]


class Solution:

	def rotate(self, N, D):
		D %= 16
		l = (N << D | N >> 16 - D) & 65535
		r = (N >> D | N << 16 - D) & 65535
		return [l, r]


class Solution:

	def rotate(self, N, D):
		BITS = 16
		n_binary_str = '{:0>{bits}}'.format(str(bin(N))[2:], bits=BITS)
		n_binary_str_list_left = list(n_binary_str)
		for j in range(0, D):
			aux = n_binary_str_list_left[BITS - 1]
			n_binary_str_list_left[BITS - 1] = n_binary_str_list_left[0]
			i = BITS - 2
			while i >= 0:
				aux2 = n_binary_str_list_left[i]
				n_binary_str_list_left[i] = aux
				aux = aux2
				i -= 1
		left = int(''.join(n_binary_str_list_left), 2)
		n_binary_str_list_right = list(n_binary_str)
		for j in range(0, D):
			aux = n_binary_str_list_right[0]
			n_binary_str_list_right[0] = n_binary_str_list_right[BITS - 1]
			i = 1
			while i <= BITS - 1:
				aux2 = n_binary_str_list_right[i]
				n_binary_str_list_right[i] = aux
				aux = aux2
				i += 1
		right = int(''.join(n_binary_str_list_right), 2)
		result = [left, right]
		return result


class Solution:

	def rotate(self, N, D):
		D = D % 16
		a = (N << D | N >> 16 - D) & 65535
		b = (N >> D | N << 16 - D) & 65535
		return (a, b)


class Solution:

	def rotate(self, N, D):
		A = [0] * 16
		D = D % 16
		i = 0
		while N > 0:
			A[i] = N % 2
			N = N // 2
			i += 1
		A.reverse()
		left = A[D:] + A[:D]
		right = A[16 - D:] + A[:16 - D]
		N = 0
		D = 0
		for i in range(15, -1, -1):
			N += left[i] * 2 ** (15 - i)
			D += right[i] * 2 ** (15 - i)
		return (N, D)


class Solution:

	def rotate(self, N, D):
		val = bin(N).replace('0b', '')
		ans = '0' * (16 - len(val)) + val
		D = D % 16
		return [int(ans[D:] + ans[:D], 2), int(ans[16 - D:] + ans[:16 - D], 2)]


class Solution:

	def rotate(self, N, D):
		l = ['0'] * (16 - len(list(bin(N)[2:]))) + list(bin(N)[2:])
		der = int(''.join(l[D % 16:] + l[0:D % 16]), 2)
		izq = int(''.join(l[-D % 16:] + l[0:-D % 16]), 2)
		return (der, izq)


class Solution:

	def rotate(self, N, D):
		x = 2 ** 16 - 1
		d = D % 16
		l = (n << d | n >> 16 - d) & x
		r = (n >> d | n << 16 - d) & x
		del x
		return [l, r]


class Solution:

	def rotate(self, N, D):
		ans = [0] * 2
		max_bits = 16
		if D > 16:
			D %= 16
		ans[0] = (N << D | N >> max_bits - D) & 65535
		ans[1] = (N >> D | N << max_bits - D) & 65535
		return ans


class Solution:

	def rotate(self, N, D):
		ans = []
		d = D % 16
		l = n << d | n >> 16 - d
		r = n >> d | n << 16 - d
		ans.append(l & 65535)
		ans.append(r & 65535)
		return ans


class Solution:

	def rotate(self, N, D):
		d = D % 16
		leftshift = (N << d | N >> 16 - d) & 65535
		rightshift = (N >> d | N << 16 - d) & 65535
		return [leftshift, rightshift]


class Solution:

	def rotate(self, N, D):
		D = D % 16
		b = bin(N)[2:]
		b = '{:>016}'.format(b)
		return (int(b[D:] + b[:D], 2), int(b[-D:] + b[:-D], 2))


class Solution:

	def rotate(self, N, D):
		left_rotated = N << D % 16 & 65535 | N >> 16 - D % 16
		right_rotated = N >> D % 16 | (N & (1 << D % 16) - 1) << 16 - D % 16
		return [left_rotated, right_rotated]


class Solution:

	def rotate(self, N, D):
		if D > 16:
			D %= 16
		A = [0] * 2
		A[0] = (N << D | N >> 16 - D) & 65535
		A[1] = (N >> D | N << 16 - D) & 65535
		return A


def rotateBits(n, d):
	return [n << d, n >> d]

class Solution:

	def rotate(self, n, d):
		d = d % 16
		a = (n << d | n >> 16 - d) & 65535
		b = (n >> d | n << 16 - d) & 65535
		return [a, b]


class Solution:

	def rotate(self, N, D):
		D = D % 16
		binary = ''
		for i in range(16):
			temp = 1 << i
			if temp & N:
				binary += '1'
			else:
				binary += '0'
		binary = binary[::-1]
		num1 = binary[D:] + binary[:D]
		num2 = binary[16 - D:] + binary[:16 - D]
		val1 = 0
		temp = 1
		for i in num1:
			val1 += 2 ** (16 - temp) * int(i)
			temp += 1
		val2 = 0
		temp = 1
		for i in num2:
			val2 += 2 ** (16 - temp) * int(i)
			temp += 1
		return [val1, val2]


class Solution:

	def rotate(self, N, D):
		D = D % 16
		N1 = bin(N)[2:]
		N1 = format(N, '016b')
		N2 = N1[D:] + N1[:D]
		N3 = N1[-D:] + N1[:-D]
		return (int(N2, 2), int(N3, 2))


class Solution:

	def rotate(self, N, D):
		b = bin(N).replace('0b', '')
		l = len(b)
		ans = ''
		lis = []
		for i in range(16 - l):
			ans += '0'
		ans += b
		ans = list(ans)
		d = D % 16
		k = 16 - d
		tem = ans.copy()
		(tem[k:], tem[:k]) = (tem[:-k], tem[-k:])
		a = ''.join(tem)
		res = int(a, 2)
		lis.append(str(res))
		(ans[:-k], ans[-k:]) = (ans[k:], ans[:k])
		ahs = ''.join(ans)
		rs = int(ahs, 2)
		lis.append(str(rs))
		return lis


class Solution:

	def rotate(self, N, D):
		ans = [N] * 2
		l1 = bin(N)[2:]
		l3 = ['0'] * (16 - len(l1))
		l3.extend(l1)
		if D > 16:
			D = D % 16
		if D > 0:
			l4 = list()
			l4.extend(l3)
			ab = 0
			for i in reversed(range(D + 1)):
				l4[-i] = l3[ab]
				ab += 1
			l4[:-D] = l3[D:]
			l5 = list()
			l5.extend(l3)
			ab = D
			for i in range(D + 1):
				l5[i] = l3[-ab]
				ab -= 1
			l5[D:] = l3[:-D]
			ans[0] = int(''.join(l4), 2)
			ans[1] = int(''.join(l5), 2)
		return ans
		return ans


class Solution:

	def rotate(self, N, D):
		D = D % 16
		left = (N << D | N >> 16 - D) & 65535
		right = (N >> D | N << 16 - D) & 65535
		return (left, right)


from collections import deque

class Solution:

	def rotate(self, N, D):
		b = bin(N)[2:]
		n = len(b)
		x = 16 - n
		b = '0' * x + b
		l = list(b)
		d1 = deque(l)
		d2 = deque(l)
		for i in range(D):
			ele = d1.popleft()
			d1.append(ele)
		for i in range(D):
			ele = d2.pop()
			d2.appendleft(ele)
		lr = ''.join(d1)
		lrv = int(lr, 2)
		rr = ''.join(d2)
		rrv = int(rr, 2)
		return (lrv, rrv)


class Solution:

	def rotate(self, N, D):
		ans = []
		bits = 16
		if D > 16:
			D %= 16
		ans.append((N << D | N >> bits - D) & 65535)
		ans.append((N >> D | N << bits - D) & 65535)
		return ans


class Solution:

	def rotate(self, N, D):
		r = ''
		ans = [0, 0]
		binaryN = bin(N)[2:]
		D = D % 16
		for i in range(16 - len(binaryN)):
			r += '0'
		r += binaryN
		rotateLeft = r[D:] + r[0:D]
		rotateRight = r[len(r) - D:] + r[:len(r) - D]
		ans[0] = int(rotateLeft, 2)
		ans[1] = int(rotateRight, 2)
		return ans


class Solution:

	def rotate(self, N, D):
		D = D % 16
		n1 = N << D | N >> 16 - D
		n2 = N >> D | N << 16 - D
		return [n1 & 65535, n2 & 65535]


class Solution:

	def rotate(self, N, D):
		leftRot = bin(N)[2:]
		leftRot = '0' * (16 - len(leftRot)) + leftRot
		rightRot = bin(N)[2:]
		rightRot = '0' * (16 - len(rightRot)) + rightRot
		D = D % 16
		leftRot = '0b' + leftRot[D:] + leftRot[:D]
		rightRot = '0b' + rightRot[-D:] + rightRot[:-D]
		return (int(leftRot, 2), int(rightRot, 2))


class Solution:

	def rotate(self, N, D):
		if D > 16:
			D = D % 16
		bin_n = bin(N)
		bin_str = str(bin_n)[2:]
		padded_str = bin_str.zfill(16)
		left_str = padded_str[D:] + padded_str[:D]
		left_n = int(left_str, 2)
		right_str = padded_str[-D:] + padded_str[:-D]
		right_n = int(right_str, 2)
		return [left_n, right_n]


class Solution:

	def rotate(self, N, D):
		ret = []
		l = self.left(N, D % 16)
		ret.append(l)
		r = self.right(N, D % 16)
		ret.append(r)
		return ret

	def newBin16(self):
		arr = []
		for i in range(16):
			arr.append(0)
		return arr

	def revBinRep16(self, N):
		arr = self.newBin16()
		i = 0
		while N != 0:
			arr[i] = N % 2
			i += 1
			N = int(N / 2)
		return arr

	def left(self, N, D):
		arr = self.revBinRep16(N)
		new_arr = self.newBin16()
		for i in range(16):
			j = i - D
			if j < 0:
				j = j + 16
			new_arr[i] = arr[j]
		new_arr.reverse()
		return int(''.join([str(x) for x in new_arr]), 2)

	def right(self, N, D):
		arr = self.revBinRep16(N)
		new_arr = self.newBin16()
		for i in range(16):
			j = i + D
			if j >= 16:
				j = j - 16
			new_arr[i] = arr[j]
		new_arr.reverse()
		return int(''.join([str(x) for x in new_arr]), 2)


class Solution:

	def rotate(self, N, D):
		ret = [0 for i in range(0, 16)]
		count = 16
		while N > 0:
			r = N % 2
			count -= 1
			ret[count] = r
			N = N // 2
		rnum = [0 for i in range(0, 16)]
		lnum = [0 for i in range(0, 16)]
		for k in range(0, 16):
			ridx = (k + D) % 16
			rnum[ridx] = ret[k]
			lidx = (k - D) % 16
			lnum[lidx] = ret[k]
		sol1 = 0
		sol2 = 0
		for t in range(0, 16):
			sol1 += 2 ** (15 - t) * rnum[t]
			sol2 += 2 ** (15 - t) * lnum[t]
		return [sol2, sol1]


class Solution:

	def rotate(self, N, D):
		a = self.rotateleft(N, D)
		b = self.rotateright(N, D)
		return [a & (1 << 16) - 1, b & (1 << 16) - 1]

	def rotateleft(self, N, D):
		D = D % 16
		x = N << D
		bits = 16 - D
		y = N >> bits
		return x | y

	def rotateright(self, N, D):
		D = D % 16
		x = N >> D
		bits = 16 - D
		y = N << bits
		return x | y


class Solution:

	def rotate(self, n, d):
		k = '{0:016b}'.format(n)
		l = []
		d = d % 16
		a = k[d:16]
		for i in range(0, d):
			a += k[i]
		l.append(int(a, 2))
		a = ''
		a = k[16 - d:16]
		for i in range(0, 16 - d):
			a += k[i]
		l.append(int(a, 2))
		return l


class Solution:

	def rotate(self, N, D):
		D = D % 16
		str = bin(N)[2:]
		str = '0' * (16 - len(str)) + str
		r_r = str[16 - D:] + str[0:16 - D]
		l_r = str[D:] + str[0:D]
		return [int(l_r, 2), int(r_r, 2)]


class Solution:

	def rotate(self, N, D):
		D = D % 16
		q = N // 2 ** (16 - D)
		q1 = N % 2 ** (16 - D)
		l = q + q1 * 2 ** D
		s = N // 2 ** D
		s1 = N % 2 ** D
		r = s + s1 * 2 ** (16 - D)
		return [l, r]


class Solution:

	def rotate(self, N, D):
		decimal = format(N, '016b')
		if D > 16:
			D = D % 16
		left_rotate = decimal[D:] + decimal[:D]
		right_rotate = decimal[-D:] + decimal[:-D]
		left_num = int(left_rotate, 2)
		right_num = int(right_rotate, 2)
		return (left_num, right_num)


class Solution:

	def rotate(self, N, D):
		n = N
		d = D
		str_bin = bin(n)[2:]
		bits = ['0'] * (16 - len(str_bin)) + list(str_bin)
		if d > 16:
			d = d % 16
		left_bits = bits[d:] + bits[:d]
		right_bits = bits[len(bits) - d:] + bits[:len(bits) - d]
		left_int = int('0b' + ''.join(left_bits), 2)
		right_int = int('0b' + ''.join(right_bits), 2)
		return (left_int, right_int)


class Solution:

	def rotate(self, N, D):
		D %= 16
		bin1 = '0' * 16
		bin = format(N, 'b')
		bin = bin1[:16 - len(bin)] + bin
		(left, right) = (bin[D:] + bin[:D], bin[len(bin) - D:] + bin[:len(bin) - D])
		return [int(left, 2), int(right, 2)]


import sys

class Solution:

	def rotate(self, N, D):
		D = D % 16
		return [(N << D | N >> 16 - D) & 65535, (N >> D | N << 16 - D) & 65535]


class Solution:

	def rotate(self, n, d):
		d = d % 16
		left = (n << d | n >> 16 - d) & 65535
		right = (n >> d | n << 16 - d) & 65535
		return [left, right]


class Solution:

	def rotate(self, N, D):
		D = D % 16
		num = bin(N)[2:]
		n = len(num)
		num = '0' * (16 - n) + num
		left = num[D:] + num[:D]
		right = num[-D:] + num[:-D]
		return (int(left, 2), int(right, 2))


class Solution:

	def rotate(self, N, D):
		D = D % 16
		s = bin(N)[2:]
		n = len(s)
		if n < 16:
			s = '0' * (16 - n) + s
		s1 = s[D:] + s[:D]
		l = [int(s1, 2)]
		s2 = s[-D:] + s[:-D]
		l += [int(s2, 2)]
		return l


class Solution:

	def rotate(self, N, D):
		a = []
		b = bin(N).replace('0b', '')
		b = b.zfill(16)
		D = D % 16
		rs = b[D:] + b[:D]
		a.append(int(rs, 2))
		ls = b[16 - D:] + b[:16 - D]
		a.append(int(ls, 2))
		return a


class Solution:

	def rotate(self, n, d):
		binary = format(n, '016b')
		binary_len = len(binary)
		d = d % binary_len
		left_rotated = int(binary[d:] + binary[:d], 2)
		right_rotated = int(binary[binary_len - d:] + binary[:binary_len - d], 2)
		return (left_rotated, right_rotated)


class Solution:

	def rotate(self, N, D):
		if D > 16:
			D = D % 16
		N_bin = bin(N)
		N_bin = N_bin[2:]
		lnb = len(N_bin)
		N_bin = '0' * (16 - lnb) + N_bin
		l_N_bin = N_bin[D:] + N_bin[:D]
		r_N_bin = N_bin[-1 * D:] + N_bin[:-1 * D]
		return [int(l_N_bin, 2), int(r_N_bin, 2)]
s = Solution()


class Solution:

	def rotate(self, N, D):
		left = N
		right = N
		for i in range(D):
			if right & 1:
				right >>= 1
				right += pow(2, 15)
			else:
				right >>= 1
			if left & pow(2, 15):
				left <<= 1
				left -= pow(2, 16)
				left += 1
			else:
				left <<= 1
		return [left, right]


class Solution:

	def rotate(self, N, D):
		D = D % 16
		p = N & 2 ** D - 1
		p = p << 16 - D
		rr1 = (p | N >> D) & 2 ** 16 - 1
		q = N & 2 ** D - 1 << 16 - D
		q = q >> 16 - D
		rl2 = (q | N << D) & 2 ** 16 - 1
		return [rl2, rr1]


class Solution:

	def rotate(self, n, d):
		k = ''
		d = d % 16
		for i in range(16):
			k = str(n >> i & 1) + k
		a = int(k[d:] + k[:d], 2)
		b = int(k[16 - d:] + k[:16 - d], 2)
		return (a, b)