Given a non-negative number N. The problem is to set the rightmost unset bit in the binary representation of N. If there are no unset bits, then just leave the number as it is.
Example 1:
Input:
N = 6
Output:
7
Explanation:
The binary representation of 6 is 110.
After setting right most bit it becomes
111 which is 7.
Example 2:
Input:
N = 15
Output:
15
Explanation:
The binary representation of 15 is 1111.
As there is no unset bit it remains the
same.
Your Task:  
You don't need to read input or print anything. Your task is to complete the function setBit() which takes an integer N as input parameter and returns the integer after setting a rightmost unset bit.
Expected Time Complexity: O(LogN)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{9}

Difficulty: EASY
Tags: Bit manipulation

Data structures
Skill types: Bit manipulation

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

Starter code:
#User function Template for python3
class Solution:
	def setBit(self, N):
		# code here

Solutions:
class Solution:

	def setBit(self, N):
		if N & N + 1:
			return N | N + 1
		return N


class Solution:

	def setBit(self, n):
		if n & n + 1:
			return n | n + 1
		return n


class Solution:

	def setBit(self, N):
		i = 0
		a = N
		while a & 1:
			i += 1
			a >>= 1
		if a > 0:
			k = 1 << i
			return N ^ k
		return N


class Solution:

	def setBit(self, N):
		n = N
		return n | n + 1 if n & n + 1 else n


class Solution:

	def setBit(self, N):
		l = N & N + 1
		if l:
			return N | N + 1
		else:
			return N


import math

class Solution:

	def setBit(self, n):
		if n & n + 1 == 0:
			return n
		return n | n + 1


import math

class Solution:

	def setBit(self, N):
		if N & N + 1 == 0:
			return N
		return N | ~(~N - 1)


class Solution:

	def setBit(self, N):
		e = bin(N)
		e = list(e)
		e = e[2:]
		e = e[::-1]
		for j in range(len(e)):
			if e[j] == '0':
				e[j] = '1'
				break
		e = e[::-1]
		e = ''.join(e)
		e = '0b' + e
		e = int(e, 2)
		return e


class Solution(object):

	def setBit(self, num):
		if num & num + 1 != 0:
			return num | num + 1
		else:
			return num


import math

class Solution:

	def setBit(self, N):
		count = 0
		n = N
		l = math.ceil(math.log(N, 2))
		while n != 0 and l >= 0:
			l -= 1
			if n & 1 == 0:
				break
			count += 1
			n >>= 1
		if l == 0:
			return N
		return N | 1 << count
110


class Solution:

	def setBit(self, N):
		n = N
		count = 0
		while n > 0:
			count += 1
			if n & 1 == 1:
				n = n >> 1
			else:
				break
		N = N | 1 << count - 1
		return N


class Solution:

	def setBit(self, N):
		cn = N
		flag = 0
		while cn & 1:
			cn = cn >> 1
			flag += 1
		if cn == 0:
			if N != 0:
				return N
			else:
				return 1
		mask = 1 << flag
		N = N | mask
		return N


class Solution:

	def setBit(self, N):
		chk = 1
		while N & chk and N > chk:
			chk = chk << 1
		return N ^ chk if N > chk else N


class Solution:

	def setBit(self, N):
		binary = bin(N).replace('0b', '')
		for i in range(len(binary)):
			if binary[-i - 1] == '0':
				return N + 2 ** i
		return N


class Solution:

	def setBit(self, N):
		c = 0
		while N & 1 << c != 0:
			c += 1
		if N >> c == 0:
			return N
		N = N | 1 << c
		return N


import math

class Solution:

	def setBit(self, n):
		mask = 1
		i = 0
		while n & mask and mask < n:
			mask = 1 << i
			i += 1
		if mask >= n:
			return n
		else:
			return mask | n


class Solution:

	def setBit(self, N):
		(i, dn) = (0, N)
		while dn > 0:
			if dn % 2 == 0:
				N += 1 << i
				break
			else:
				i += 1
			dn = dn >> 1
		return N


class Solution:

	def setBit(self, N):
		x = bin(N)
		y = ''
		f = 0
		for i in range(1, len(x) + 1):
			if f == 0 and x[-i] == '0':
				y = '1' + y
				f = 1
			else:
				y = x[-i] + y
		return int(y[2:], 2)


class Solution:

	def setBit(self, N):
		mask = 1
		ans = N
		while N != 0:
			if N % 2 == 0:
				return ans | mask
			mask = mask << 1
			N = N >> 1
		return ans


class Solution:

	def setBit(self, N):
		if N & N + 1 == 0:
			return N
		else:
			return N | N + 1


class Solution:

	def setBit(self, N):
		(temp, i) = (N, 0)
		while temp:
			if temp & 1 == 0:
				N = N | 1 << i
				break
			i += 1
			temp >>= 1
		return N


class Solution:

	def setBit(self, N):
		n = list(bin(N)[2:])
		n = n[::-1]
		for i in range(len(n)):
			if n[i] == '0':
				n[i] = '1'
				break
		return int(''.join(n[::-1]), 2)


class Solution:

	def setBit(self, N):
		x = 0
		while 1 << x <= N:
			if 1 << x & N:
				x = x + 1
				continue
			else:
				N = N | 1 << x
				break
		return N


class Solution:

	def setBit(self, N):
		c = 0
		t = N
		while t > 0:
			if t & 1 == 0:
				break
			t = t >> 1
			c = c + 1
		if t != 0:
			m = 1 << c
			return N | m
		return N


import math

class Solution:

	def getPosofRightmostSetBit(self, n):
		return int(math.log2(n & -n) + 1)

	def setBit(self, N):
		n = N
		if n == 0:
			return 1
		if n & n + 1 == 0:
			return n
		pos = self.getPosofRightmostSetBit(~n)
		return 1 << pos - 1 | n


class Solution:

	def setBit(self, N):

		def findPrevPower(n):
			pos = -1
			while n != 0:
				n = n >> 1
				pos += 1
			return pos
		count = 0
		n = N
		while n & 1 != 0:
			n = n >> 1
			count += 1
		if count == findPrevPower(N) + 1:
			return N
		return N | 1 << count


class Solution:

	def setBit(self, N):
		i = 0
		while 1 << i <= N:
			if not 1 << i & N:
				return N ^ 1 << i
			i += 1
		return N


class Solution:

	def setBit(self, N):
		pos = 0
		temp = N
		while temp > 0:
			if temp & 1 == 0:
				return N | 1 << pos
			pos += 1
			temp >>= 1
		return N


class Solution:

	def setBit(self, n):
		i = 1
		while i < n:
			if n & i == 0:
				return i | n
			else:
				i = i << 1
		return n


class Solution:

	def setBit(self, N):
		ans = str(bin(N).replace('0b', ''))
		i = 1
		while i < len(ans):
			if ans[-i] == '0':
				ans[-i] == '1'
				return N + 2 ** (i - 1)
			i += 1
		return N


class Solution:

	def setBit(self, N):
		res = N | N + 1 if N & N + 1 else N
		return res


class Solution:

	def setBit(self, N):
		change = N
		count = 0
		stop = len(str(bin(N))) - 2
		while N == change:
			if count == stop:
				break
			x = 1 << count
			change = N | x
			count += 1
		return change


class Solution:

	def setBit(self, N):
		a = bin(N).replace('0b', '')
		if a.count('1') == len(a):
			return N
		a = a[::-1]
		for i in range(len(a)):
			if a[i] == '0':
				c = N | 1 << i
				break
		return c


class Solution:

	def setBit(self, r):
		if r & r + 1 == 0:
			return r
		return r | r + 1


class Solution:

	def setBit(self, r):
		N = r
		c = 0
		while N:
			if N & 1 == 0:
				break
			N >>= 1
			c += 1
		return 1 << c | r if 1 << c <= r else r


class Solution:

	def setBit(self, N):
		firstUnset = -1
		lastSet = -1
		temp = N
		i = 0
		while temp:
			bit = temp % 2
			temp = temp // 2
			if bit:
				lastSet = i
			elif firstUnset == -1:
				firstUnset = i
			i += 1
		if firstUnset <= lastSet and firstUnset >= 0:
			return N | 1 << firstUnset
		else:
			return N


class Solution:

	def setBit(self, n):
		rightmost = -1
		copy = n
		i = 0
		while True:
			if copy & 1 == 0:
				return n + 2 ** i
			else:
				copy = copy >> 1
				i += 1
			if 2 ** i > n:
				break
		return n


class Solution:

	def setBit(self, N):
		k = 0
		temp = N
		found = False
		while temp:
			digit = temp % 2
			temp = temp // 2
			if digit == 0:
				found = True
				break
			else:
				k += 1
		if found:
			return N | 1 << k
		else:
			return N


class Solution:

	def setBit(self, n):
		a = n
		c = 0
		while a:
			if a & 1 == 0:
				return n | 1 << c
			c += 1
			a = a >> 1
		return n


class Solution:

	def setBit(self, N):
		temp = N
		cnt = 0
		s = bin(N).replace('0b', '')
		if s.count('0') == 0:
			return N
		while temp:
			if temp & 1 == 1:
				cnt += 1
				temp = temp >> 1
			else:
				return N | 1 << cnt


class Solution:

	def setBit(self, n):
		if n == 0:
			return 1
		elif n & n + 1 == 0:
			return n
		else:
			store = n
			i = 0
			while store:
				if store % 2 == 0:
					break
				else:
					i = i + 1
					store = store >> 1
			bitmask = 1 << i
			return n | bitmask


class Solution:

	def setBit(self, n):
		a = bin(n)[2:]
		x = a.count('1')
		if len(a) == x:
			return n
		return n | n + 1


class Solution:

	def setBit(self, N):
		for i in range(32):
			if N & 1 << i == 0 and 1 << i <= N:
				N = N | 1 << i
				break
		return N


class Solution:

	def setBit(self, n):
		m = 31
		f = 0
		l = 0
		for i in range(31, -1, -1):
			if not f:
				if n >> i & 1:
					l = i
					f = 1
			if n >> i & 1 == 0:
				m = i
		if l < m:
			return n
		return n | 1 << m


class Solution:

	def setBit(self, N):
		a = bin(N).replace('0b', '')
		temp = []
		for i in range(len(a)):
			temp.append(a[i])
		for i in range(len(temp) - 1, -1, -1):
			if temp[i] == '0':
				temp[i] = '1'
				a = ''.join(temp)
				return int(a, 2)
		return N


class Solution:

	def setBit(self, N):
		n = N
		idx = 0
		while n > 0:
			if n & 1 == 0:
				break
			idx += 1
			n = n >> 1
		if n == 0:
			return N
		return N + (1 << idx)


class Solution:

	def setBit(self, n):
		i = 0
		rmub = -1
		no = n
		while no:
			if no & 1 == 0:
				rmub = i
				break
			no = no >> 1
			i += 1
		if rmub == -1:
			return n
		mask = 1 << rmub
		n = n | mask
		return n


class Solution:

	def setBit(self, N):
		count = 0
		x = N
		bool = False
		while x:
			if x & 1 == 0:
				bool = True
				break
			count += 1
			x = x >> 1
		mask = 1 << count
		if bool == False:
			return N
		return N | mask


class Solution:

	def setBit(self, N):
		x = 0
		y = N
		while N % 2 == 1 and N > 1:
			x += 1
			N = N >> 1
		return y | 1 << x


class Solution:

	def setBit(self, N):
		k = bin(N)
		k = k[2:]
		k = list(k)
		if k.count('0') == 0:
			return N
		else:
			s = 0
			k = k[::-1]
			for i in range(len(k)):
				if k[i] == '0':
					k[i] = '1'
					break
			for i in range(len(k)):
				s += int(2 ** i) * int(k[i])
			return s


class Solution:

	def setBit(self, N):
		s = bin(N)[2:]
		for i in range(len(s) - 1, -1, -1):
			if s[i] == '0':
				s = s[:i] + '1' + s[i + 1:]
				break
		return int(s, 2)


class Solution:

	def setBit(self, N):
		if not N & N + 1:
			return N
		else:
			return N | N + 1


class Solution:

	def setBit(self, n):
		s = ''
		i = 0
		res = 0
		while n > 0:
			x = n & 1
			if x == 0 and i == 0:
				s += str(1)
				i += 1
				n = n >> 1
			else:
				s += str(x)
				n = n >> 1
		for i in range(len(s)):
			res += pow(2, i) * int(s[i])
		return res


class Solution:

	def setBit(self, n):
		for bit in range(32):
			if 1 << bit < n and (not n & 1 << bit):
				n = n + (1 << bit)
				break
		return n


class Solution:

	def setBit(self, N):
		x = bin(N)[2:]
		new = ''
		count = 0
		for i in x[::-1]:
			if i == '0' and count == 0:
				count += 1
				new += '1'
			else:
				new += i
		new = new[::-1]
		return int(new, 2)


class Solution:

	def setBit(self, N):
		if N == 0:
			return 1
		temp = N
		index = 0
		while temp:
			if temp & 1:
				temp = temp // 2
				index += 1
			else:
				break
		if temp == 0:
			return N
		mask = 1 << index
		return N ^ mask


class Solution:

	def setBit(self, N):
		pos = 0
		flag = 0
		ans = N
		while N != 0:
			if N & 1 == 1:
				pos += 1
			else:
				flag = 1
				break
			N = N >> 1
		if flag == 1:
			ans = ans | 1 << pos
		return ans


class Solution:

	def setBit(self, N):
		if N == 0:
			return 1
		if N & N + 1 == 0:
			return N
		pos = ~N & N + 1
		return N + pos


class Solution:

	def setBit(self, N):
		temp = N | N + 1
		if temp > N << 1:
			return N
		else:
			return temp


class Solution:

	def setBit(self, N):
		binary = list(map(str, bin(N)[2:]))
		n = len(binary)
		for i in range(n):
			if binary[n - i - 1] == '0':
				binary[n - i - 1] = '1'
				return int(''.join(binary), 2)
		return N


class Solution:

	def setBit(self, N):
		if N & N + 1 == 0:
			return N
		x = N | N + 1
		return x


class Solution:

	def setBit(self, N):
		c = 0
		N1 = N
		while N:
			if N % 2 == 0:
				N1 = N1 | 1 << c
				break
			c += 1
			N = N // 2
		return N1


class Solution:

	def setBit(self, N):
		tmp = N
		mask = 1
		while tmp != 0:
			if tmp & mask == 0:
				break
			mask <<= 1
		if mask == N + 1:
			return N
		return N | mask