Given an integer N, write a program to find the one’s complement of the integer.
 
Example 1:
Input:
N = 5
Output:
2
Explanation:
binary of 5 is 101
1's complement of 101 is 010
010 is 2 in its decimal form. 
Example 2:
Input:
N = 255
Output:
0
Explanation:
binary of 255 is 1111 1111
1's complement of 1111 1111 is
0000 0000 which is 0 in its decimal form.
Your Task:
You don't need to read input or print anything. Your task is to complete the function onesComplement() which takes an integer N as input parameter and returns an integer value, the one's complement of N.
 
Expected Time Complexity: O(1)
Expected Space Complexity: O(1)
 
Constraints:
1 <= N <= 10^{6}

Difficulty: EASY
Tags: Bit manipulation

Data structures

Mathematics
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 onesComplement(self,N):
        #code here

Solutions:
class Solution:

	def onesComplement(self, N):
		binario = bin(N)[2:]
		complemento = ''
		for l in binario:
			complemento += str(1 - int(l))
		return int(complemento, 2)


class Solution:

	def onesComplement(self, N):
		d = str(bin(N).replace('0b', ''))
		s = ''
		for i in d:
			if i == '1':
				s += '0'
			else:
				s += '1'
		return int(s, 2)


class Solution:

	def onesComplement(self, N):
		b = bin(N)[2:]
		s = ''
		for i in b:
			if i == '0':
				s += '1'
			if i == '1':
				s += '0'
		return int(s, 2)


class Solution:

	def onesComplement(self, N):
		a = format(N, 'b')
		s = ''
		for i in a:
			if i == '0':
				s = s + '1'
			else:
				s = s + '0'
		ans = int(s, 2)
		return ans


class Solution:

	def onesComplement(self, N):
		ans = 0
		l = []
		while N:
			if N % 2:
				l.append(0)
			else:
				l.append(1)
			N //= 2
		while len(l) > 0:
			ans = ans * 2 + l.pop()
		return ans


class Solution:

	def onesComplement(self, N):
		newnum = bin(N)[2:]
		ans = ''
		for i in range(0, len(newnum)):
			if newnum[i] == '0':
				ans += '1'
			else:
				ans += '0'
		return int(ans, 2)


class Solution:

	def onesComplement(self, N):
		z = bin(N)[2:]
		x = ''
		for i in z:
			if i == '0':
				x += '1'
			else:
				x += '0'
		return int(x, 2)


class Solution:

	def onesComplement(self, N):
		s = str(bin(N)).replace('0b', '')
		s = s.replace('0', 'k')
		s = s.replace('1', '0')
		s = s.replace('k', '1')
		return int(s, 2)


class Solution:

	def onesComplement(self, N):
		num_bits = N.bit_length()
		mask = (1 << num_bits) - 1
		ones_complement = N ^ mask
		return ones_complement


class Solution:

	def onesComplement(self, N):
		num = bin(N)[2:]
		output = ''
		for item in num:
			if item == '1':
				output += '0'
			else:
				output += '1'
		return int(output, 2)


class Solution:

	def onesComplement(self, N):
		(res, x) = (0, 1)
		while N != 0:
			if N % 2 == 0:
				res += x
			x = x * 2
			N = N >> 1
		return res


class Solution:

	def onesComplement(self, N):
		c = 0
		n = N
		while n > 0:
			c += 1
			n = n // 2
		return 2 ** c - 1 - N


class Solution:

	def onesComplement(self, N):
		pw = 1
		cnt = 0
		if N == 1:
			return 0
		while pw <= N:
			pw *= 2
			cnt += 1
		return 2 ** cnt - 1 - N


class Solution:

	def onesComplement(self, N):
		m = N
		mask = 0
		while m != 0:
			mask = mask << 1 | 1
			m = m >> 1
		return ~N & mask


class Solution:

	def onesComplement(self, N):
		s = bin(N)
		s = s[2:]
		s = s.replace('0', '*')
		s = s.replace('1', '0')
		s = s.replace('*', '1')
		return int(s, 2)


class Solution:

	def onesComplement(self, N):
		temp = bin(N)[2:]
		temp = temp.replace('0', '$')
		temp = temp.replace('1', '0')
		temp = temp.replace('$', '1')
		return int(str(temp), 2)


class Solution:

	def onesComplement(self, N):
		bin = format(N, 'b')
		bin = bin.translate(str.maketrans('10', '01'))
		return int(bin, 2)


import math

class Solution:

	def onesComplement(self, n):
		number_of_bits = int(math.floor(math.log(n) / math.log(2))) + 1
		return (1 << number_of_bits) - 1 ^ n


class Solution:

	def onesComplement(self, N):
		j = 0
		a = N
		while a:
			j += 1
			a = a >> 1
		i = 0
		while j:
			N = N ^ 1 << i
			i += 1
			j -= 1
		return N


class Solution:

	def onesComplement(self, N):
		binary = '{0:b}'.format(N)
		binary_comple = ''
		for i in binary:
			if i == '0':
				binary_comple += '1'
			else:
				binary_comple += '0'
		return int(binary_comple, 2)


class Solution:

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


class Solution:

	def onesComplement(self, N):
		num = ~N
		mask = 0
		if N == 0:
			return 1
		while N != 0:
			N = N >> 1
			mask = mask << 1
			mask = mask | 1
		return mask & num


class Solution:

	def onesComplement(self, n):
		bn = bin(n)
		s = ''
		for i in bn[1:]:
			if i == '1':
				s += '0'
			elif i == '0':
				s += '1'
		return int(s, 2)


class Solution:

	def onesComplement(self, N):
		l = []
		while N:
			l.append(N & 1)
			N >>= 1
		l = l[::-1]
		for i in range(len(l)):
			if l[i] == 1:
				l[i] = 0
			else:
				l[i] = 1
		base = 1
		N = 0
		for i in range(len(l) - 1, -1, -1):
			N += l[i] * base
			base *= 2
		return N


import math

class Solution:

	def onesComplement(self, N):
		n = int(math.log2(N) + 1)
		x = pow(2, n) - 1
		return N ^ x


class Solution:

	def onesComplement(self, N):
		c = 1
		k = 2
		while k <= N:
			k *= 2
			c += 1
		return 2 ** c - N - 1


class Solution:

	def onesComplement(self, N):
		temp = N
		count = 0
		while temp:
			count = count + 1
			temp = temp >> 1
		return N ^ (1 << count) - 1


class Solution:

	def onesComplement(self, N):
		b = bin(N)[2:]
		r = ''
		for x in b:
			if x == '1':
				r += '0'
			else:
				r += '1'
		return int(r, 2)


class Solution:

	def onesComplement(self, N):
		b = bin(N).replace('0b', '')
		s = str(b)
		r = ''
		for i in range(len(s)):
			if s[i] == '0':
				r = r + '1'
			else:
				r = r + '0'
		i = int(r, 2)
		return i


class Solution:

	def onesComplement(self, N):
		m = N
		mask = 0
		if m == 0:
			return 1
		while m != 0:
			mask = mask << 1 | 1
			m = m >> 1
		ans = ~N & mask
		return ans
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		N = int(input())
		ob = Solution()
		print(ob.onesComplement(N))


class Solution:

	def onesComplement(self, N):
		bnum = '{0:b}'.format(N)
		s = ''
		for i in bnum:
			if i == '1':
				s += '0'
			else:
				s += '1'
		return int(s, 2)
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		N = int(input())
		ob = Solution()
		print(ob.onesComplement(N))


class Solution:

	def onesComplement(self, N):
		m = bin(N)
		m = m[2:]
		m = m.replace('1', 'a')
		m = m.replace('0', '1')
		m = m.replace('a', '0')
		return int(m, 2)
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		N = int(input())
		ob = Solution()
		print(ob.onesComplement(N))


import math

class Solution:

	def onesComplement(self, N):
		n = math.floor(math.log(N, 2)) + 1
		return (1 << n) - 1 ^ N
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		N = int(input())
		ob = Solution()
		print(ob.onesComplement(N))


class Solution:

	def onesComplement(self, N):
		d = bin(N)
		d = d[2:]
		n = len(d) - 1
		t = ''
		for i in d:
			if i == '0':
				t += '1'
			else:
				t += '0'
		c = 0
		for i in t:
			if i == '1':
				c += 2 ** n
			n -= 1
		return c
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		N = int(input())
		ob = Solution()
		print(ob.onesComplement(N))


class Solution:

	def onesComplement(self, N):
		x = 1
		N ^= 1
		while x <= N:
			x <<= 1
			N ^= x
		return N
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		N = int(input())
		ob = Solution()
		print(ob.onesComplement(N))


class Solution:

	def onesComplement(self, N):
		bin_n = str(bin(N)).replace('0b', '')
		com_n = ''
		for i in bin_n:
			if i == '1':
				com_n += '0'
			else:
				com_n += '1'
		return int(com_n, 2)


class Solution:

	def onesComplement(self, N):
		v = []
		while N != 0:
			v.append(N % 2)
			N = N // 2
		v.reverse()
		for i in range(len(v)):
			if v[i] == 0:
				v[i] = 1
			else:
				v[i] = 0
		p = 1
		for i in range(len(v) - 1, -1, -1):
			N = N + v[i] * p
			p *= 2
		return N


class Solution:

	def onesComplement(self, N):
		(bit_count, n) = (0, N)
		while n > 0:
			bit_count += 1
			n = n >> 1
		all_bits_set = pow(2, bit_count) - 1
		return all_bits_set ^ N


class Solution:

	def onesComplement(self, N):
		S = bin(N)[2:]
		result = 0
		binary_result = ''
		for i in S:
			if i == '0':
				binary_result += '1'
			else:
				binary_result += '0'
		binary_result = binary_result[::-1]
		for i in range(len(binary_result)):
			if binary_result[i] == '1':
				result += 2 ** i
		return result


import math

class Solution:

	def onesComplement(self, N):
		l = int(math.log(N) / math.log(2) + 1)
		return (1 << l) - 1 ^ N


class Solution:

	def onesComplement(self, N):
		s = [i for i in str(bin(N))][2:]
		for i in range(len(s)):
			if s[i] == '0':
				s[i] = '1'
			elif s[i] == '1':
				s[i] = '0'
		return int(''.join(s), 2)


class Solution:

	def onesComplement(self, N):
		import math
		return 2 ** int(math.log2(N) + 1) - 1 ^ N


class Solution:

	def onesComplement(self, N):
		x = bin(N)[2:]
		x = str(x)
		x = list(x)
		arr = ''
		for i in x:
			if i == '1':
				arr += '0'
			else:
				arr += '1'
		return int(arr, 2)


import math

class Solution:

	def onesComplement(self, N):
		siz = self.getLength(N)
		mask = (1 << siz) - 1
		return N ^ mask

	def getLength(self, n):
		return int(math.log2(n)) + 1


class Solution:

	def onesComplement(self, N):
		result = index = 0
		while N:
			currentBit = N & 1
			complement = not currentBit
			result = complement << index | result
			index += 1
			N = N >> 1
		return result


import math

class Solution:

	def onesComplement(self, N):
		b = int(math.log(N) / math.log(2) + 1)
		num = (1 << b) - 1
		return N ^ num


import math

class Solution:

	def onesComplement(self, N):
		bits = math.floor(math.log(N, 2)) + 1
		return N ^ (1 << bits) - 1


class Solution:

	def dtob(self, n):
		p = 1
		b = 0
		d = 0
		while n != 0:
			d = n % 2
			n = n // 2
			b = b + d * p
			p = p * 10
		return b

	def onesComplement(self, N):
		x = N
		w = len(str(self.dtob(N)))
		temp = (1 << w) - 1
		return x ^ temp


class Solution:

	def onesComplement(self, n):
		s = ''
		while n:
			if n & 1:
				s = '0' + s
			else:
				s = '1' + s
			n = n >> 1
		return int(s, 2)


import math

class Solution:

	def onesComplement(self, N):
		res = N
		for i in range(int(math.log(N, 2)) + 1):
			res = res ^ 1 << i
		return res


class Solution:

	def onesComplement(self, N):
		d = bin(N).replace('0b', '')
		c = ''
		for i in range(len(d)):
			if d[i] == '0':
				c += '1'
			else:
				c += '0'
		return int(c, 2)


class Solution:

	def onesComplement(self, N):
		Bin = str(int(bin(N)[2:]))
		ans = ''
		for i in Bin:
			if i == '0':
				ans += '1'
			else:
				ans += '0'
		return int(ans, 2)


class Solution:

	def onesComplement(self, N):
		x = bin(N).replace('0b', '')
		ans = ''
		for i in range(len(x)):
			if x[i] == '1':
				ans += '0'
			else:
				ans += '1'
		return int(ans, 2)


import math

class Solution:

	def onesComplement(self, n):
		bits = int(math.log(n, 2)) + 1
		return n ^ 2 ** bits - 1


class Solution:

	def onesComplement(self, N):
		a = 0
		if N == 1:
			return 0
		while 2 ** a <= N:
			a = a + 1
		return 2 ** a - 1 ^ N


class Solution:

	def onesComplement(self, N):
		p = bin(N).replace('0b', '')
		a = str(p)
		l = []
		for i in a:
			if i == '0':
				l.append('1')
			else:
				l.append('0')
		b = ''.join(l)
		d = int(b, 2)
		return d


class Solution:

	def onesComplement(self, N):
		b = '{0:b}'.format(int(N))
		K = ''
		for i in str(b):
			if i == '0':
				K += '1'
			elif i == '1':
				K += '0'
		return int(K, 2)


from math import log

class Solution:

	def onesComplement(self, N):
		if log(N + 1, 2) == int(log(N + 1, 2)):
			x = int(log(N + 1, 2))
		else:
			x = int(log(N + 1, 2)) + 1
		return pow(2, x) - N - 1


class Solution:

	def onesComplement(self, n):
		r = list(bin(n)[2:])
		r.reverse()
		s = 0
		for i in range(len(r)):
			if r[i] == '0':
				s += 2 ** i
		return s


class Solution:

	def onesComplement(self, N):
		b = bin(N)[2:]
		mask = bin(pow(2, len(b)) - 1)[2:]
		return int(b, 2) ^ int(mask, 2)


class Solution:

	def onesComplement(self, N):
		bin1 = bin(N).replace('0b', '')
		lst = list(str(bin1))
		for i in range(len(lst)):
			if lst[i] == '0':
				lst[i] = '1'
			elif lst[i] == '1':
				lst[i] = '0'
		a = ''.join(lst)
		return int(a, 2)