Given a number N.Find the nearest number which is a power of 2 and greater than or equal to N.
Example 1:
Input:
N = 1
Output:
1
Explanation:
The nearest power of 2 greater than or 
equal to 1 is 2^0=1.
Example 2:
Input:
N = 5
Output:
8
Explanation:
The nearest power of 2 greater than 5 is 
2^3=8.
Your Task:
You don't need to read input or print anything.Your Task is to complete the function nearestPowerOf2() which takes a number N as input parameter and returns the nearest power of 2 that is greater than or equal to N.
Expected Time Complexity:O(LogN)
Expected Auxillary Space:O(1)
Constraints:
1<=N<=10^{18}

Source: geeksforgeeks | Difficulty: EASY
Tags: Bit manipulation

Data structures

Starter code: #User function Template for python3

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

Solutions:
class Solution:

	def nearestPowerOf2(self, N):
		count = 0
		n = N
		if n & n - 1 == 0:
			return N
		while n != 0:
			count += 1
			n >>= 1
		return 1 << count


class Solution:

	def nearestPowerOf2(self, N):
		if N == 0:
			return 1
		N -= 1
		N |= N >> 1
		N |= N >> 2
		N |= N >> 4
		N |= N >> 8
		N |= N >> 16
		N |= N >> 32
		N += 1
		return N


class Solution:

	def nearestPowerOf2(self, N):
		p = 1
		while N > p:
			p *= 2
		return p


class Solution:

	def nearestPowerOf2(self, N):
		for i in range(N):
			if 2 ** i >= N:
				return 2 ** i


import math

class Solution:

	def nearestPowerOf2(self, N):
		i = 0
		while 2 ** i < N:
			i += 1
		return 2 ** i


from math import ceil

class Solution:

	def nearestPowerOf2(self, N):
		count = 0
		while N > 1:
			count += 1
			N = ceil(N / 2)
		return 2 ** count


from math import log2

class Solution:

	def nearestPowerOf2(self, N):
		if N & N - 1 == 0:
			return N
		N = int(bin(N)[2:].replace('1', '0').replace('0', '1', 1), 2)
		return N << 1


class Solution:

	def nearestPowerOf2(self, N):
		j = 0
		d = N
		while d > 0:
			d = int(d / 2)
			j = j + 1
		if 2 ** j / 2 == N:
			return N
		else:
			return 2 ** j


class Solution:

	def nearestPowerOf2(self, N):
		res = 1
		while res < N:
			res = res << 1
		return res


class Solution:

	def nearestPowerOf2(self, N):
		c = 0
		N -= 1
		while N:
			N //= 2
			c += 1
		return 1 << c


import math

class Solution:

	def nearestPowerOf2(self, N):
		if N & N - 1:
			x = math.floor(math.log2(N)) + 1
			return pow(2, x)
		return N


class Solution:

	def nearestPowerOf2(self, N):
		flag = 1
		i = 0
		while flag:
			if 2 ** i >= N:
				return 2 ** i
			i += 1


class Solution:

	def nearestPowerOf2(self, N):
		i = 1
		while i < N:
			i *= 2
		return i


class Solution:

	def nearestPowerOf2(self, N):
		i = 0
		mul = 1
		for i in range(N):
			if mul >= N:
				return mul
			mul *= 2
		return -1


class Solution:

	def nearestPowerOf2(self, N):
		cnt = 0
		cnt_one = 0
		m = N
		while N > 0:
			if N & 1 == 1:
				cnt_one += 1
			cnt += 1
			N >>= 1
		if cnt_one == 1:
			return m
		else:
			res = 2 ** cnt
			return res


class Solution:

	def nearestPowerOf2(self, N):
		if N <= 1:
			return 1
		a = 1
		for i in range(N):
			c = a * 2
			if c >= N:
				return c
			else:
				a = c


class Solution:

	def nearestPowerOf2(self, N):
		k = 1
		while k < N:
			k = k << 1
		return k


class Solution:

	def nearestPowerOf2(self, N):
		if N == 1:
			return 1
		if N & N - 1 == 0:
			return N
		while N & N - 1:
			N = N & N - 1
		return N << 1


class Solution:

	def nearestPowerOf2(self, N):
		if N & N - 1 == 0:
			return N
		else:
			return 1 << N.bit_length()


import math

class Solution:

	def nearestPowerOf2(self, N):
		if N == 1:
			return N
		else:
			return pow(2, math.ceil(math.log(N, 2)))


class Solution:

	def nearestPowerOf2(self, n):
		if n & n - 1 == 0 and n:
			return n
		return int('1' + len(bin(n)[2:]) * '0', 2)


class Solution:

	def nearestPowerOf2(self, n):
		if n & n - 1 == 0 and n:
			return n
		p = 1
		while p < n:
			p <<= 1
		return p


class Solution:

	def nearestPowerOf2(self, N):
		if N == 1:
			return N
		temp = N
		count = -1
		while temp:
			count = count + 1
			temp = temp >> 1
		if N == 1 << count:
			return N
		else:
			return 1 << count + 1


class Solution:

	def nearestPowerOf2(self, N):
		w = 1
		while w < N:
			w *= 2
		return w


import math

class Solution:

	def nearestPowerOf2(self, N):
		for i in range(N):
			if 2 ** i >= N:
				return 2 ** i
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		N = int(input())
		ob = Solution()
		print(ob.nearestPowerOf2(N))


import math

class Solution:

	def nearestPowerOf2(self, N):
		x = int(math.log(N, 2))
		if 2 ** x == N:
			return N
		return 2 ** (x + 1)
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		N = int(input())
		ob = Solution()
		print(ob.nearestPowerOf2(N))


class Solution:

	def nearestPowerOf2(self, N):
		c = 0
		d = 1
		while d < N:
			d = 2 ** c
			c += 1
		return d
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		N = int(input())
		ob = Solution()
		print(ob.nearestPowerOf2(N))


class Solution:

	def nearestPowerOf2(self, N):
		count = 0
		if N and (not N & N - 1):
			return N
		while N != 0:
			N >>= 1
			count += 1
		return 1 << count


import math

class Solution:

	def nearestPowerOf2(self, N):
		if N == 1:
			return 1
		log = math.log10(N) / math.log10(2)
		if math.ceil(log) == math.floor(log):
			return N
		return int(''.join(['1'] + ['0'] * (len(bin(N)) - 2)), 2)


import math

class Solution:

	def nearestPowerOf2(self, N):
		res = pow(2, int(math.log(N, 2) + 1))
		if N == 1:
			return 1
		if N & N - 1 == 0:
			return N
		return res


class Solution:

	def nearestPowerOf2(self, N):
		for i in range(N):
			powerOfTwo = 1 << i
			if powerOfTwo >= N:
				return powerOfTwo
		return 1


class Solution:

	def nearestPowerOf2(self, N):
		count = 0
		while 1 << count < N:
			count += 1
		return 1 << count


class Solution:

	def nearestPowerOf2(self, N):
		if N == 1:
			return 1
		i = 1
		while True:
			i = i << 1
			if N <= i:
				return i


import math

class Solution:

	def nearestPowerOf2(self, N):
		bits = int(math.log(N, 2)) + 1
		if 2 ** (bits - 1) == N:
			return N
		return 2 ** bits


import math

class Solution:

	def nearestPowerOf2(self, n):
		p = int(math.log(n) / math.log(2))
		if 2 ** p == n:
			return n
		return 2 ** (p + 1)


import math

class Solution:

	def nearestPowerOf2(self, N):
		ans = int(math.log2(N))
		if N == 1 << ans:
			return N
		else:
			return 1 << ans + 1


class Solution:

	def nearestPowerOf2(self, N):
		ones = 0
		current = 0
		while N > 1:
			if N & 1 == 1:
				ones = ones + 1
			N = N >> 1
			current = current + 1
		if ones > 0:
			return 2 ** (current + 1)
		return 2 ** current


from math import ceil, log2

class Solution:

	@staticmethod
	def nearestPowerOf2(N):
		return 1 << ceil(log2(N))