Given a number 32 bit number N .Find the number you get by inverting bits in its binary representation (i.e. 1 is made 0 and 0 is made 1). In other words,Negate(~) the number.
Example 1:
Input:
N=4289384
Output:
4290677911
Explanation:
00000000010000010111001101101000
is the binary representation of 4289384.
Negating this in 32 bits, we get
11111111101111101000110010010111
which is the binary representation of
4290677911.
Example2:
Input:
N=1
Output:
4294967294
Explanation:
Binary representation of 1 is
00000000000000000000000000000001.
Negating this we get
11111111111111111111111111111110
which is the binary representation of
4294967294.
Your task:
You don't need to read input or print anything.Your task is to complete the function invertBits() which takes a number N as input parameter and returns the negation of the number.
Expected Time Complexity:O(1)
Expected Auxillary Space:O(1)
Constraints:
1<=N<=10^{8}+9

Source: geeksforgeeks | Difficulty: EASY
Tags: Bit manipulation

Data structures

Starter code: #User function Template for python3

class Solution:
    def invertBits(self,bin):
        #code here

Solutions:
class Solution:

	def invertBits(self, bin):
		return 2 ** 32 - bin - 1


class Solution:

	def invertBits(self, x):
		t = bin(x)[2:]
		t = '0' * (32 - len(t)) + t
		new = ''
		for i in t:
			if i == '0':
				new += '1'
			else:
				new += '0'
		return int(new, 2)


class Solution:

	def invertBits(self, N):
		k = bin(N)[2:]
		s1 = (32 - len(k)) * '0' + k
		s = ''
		for i in s1:
			if i == '1':
				s += '0'
			else:
				s += '1'
		return int(s, 2)


class Solution:

	def invertBits(self, n):
		num = bin(n)[2:]
		output = '1' * (32 - len(num))
		for item in num:
			if item == '1':
				output += '0'
			else:
				output += '1'
		return int(output, 2)


class Solution:

	def invertBits(self, bin):
		return 4294967295 ^ bin


class Solution:

	def invertBits(self, a):
		return 4294967295 ^ a


class Solution:

	def invertBits(self, N):
		N = bin(N).replace('0b', '').zfill(32)
		N = N.replace('1', '2')
		N = N.replace('0', '1')
		N = N.replace('2', '0')
		return int(N, 2)


class Solution:

	def invertBits(self, N):
		return N ^ (1 << 32) - 1


class Solution:

	def invertBits(self, N):
		val = format(N, '032b')
		return N ^ (1 << 32) - 1


class Solution:

	def invertBits(self, n):
		a = bin(n).replace('0b', '')
		l = len(a)
		for i in range(32 - len(a)):
			a = '0' + a
		r = ''
		for i in range(len(a)):
			if a[i] == '0':
				r += '1'
			else:
				r += '0'
		return int(r, 2)


class Solution:

	def invertBits(self, N):
		S = list(bin(N)[2:])
		while len(S) < 32:
			S.insert(0, '0')
		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 invertBits(self, n):
		w = bin(n)[2:]
		d = 32 - len(w)
		h = '0' * d + w
		m = h.replace('0', '*').replace('1', '0').replace('*', '1')
		return int(m, 2)


class Solution:

	def invertBits(self, bin):
		return int(pow(2, 32) - 1) ^ bin


class Solution:

	def invertBits(self, N):
		x = '{:032b}'.format(N)
		x = x.replace('1', 'a')
		x = x.replace('0', '1')
		x = x.replace('a', '0')
		return int(x, 2)
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		N = int(input())
		ob = Solution()
		print(ob.invertBits(N))


class Solution:

	def invertBits(self, bin):
		return 4294967296 - 1 - bin
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		N = int(input())
		ob = Solution()
		print(ob.invertBits(N))


class Solution:

	def invertBits(self, n):
		x = 1
		for i in range(32):
			x *= 2
		x -= 1
		return x ^ N


import math

class Solution:

	def invertBits(self, n):
		mask = (1 << 32) - 1
		return n ^ mask


import math

class Solution:

	def invertBits(self, n):
		result = index = 0
		siz = self.getLength(n)
		while n:
			currentBit = not n & 1
			result = result | currentBit << index
			n = n >> 1
			index += 1
		binaryStr = self.decimalToBinary(result)
		binaryStr = self.fill(binaryStr, '0', siz)
		binaryStr = self.fill(binaryStr, '1', 32)
		return int(binaryStr, 2)

	def decimalToBinary(self, n):
		state = ''
		while n:
			state = str(n & 1) + state
			n = n >> 1
		return state

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

	def fill(self, binaryStr, paddedStr, width):
		siz = width - len(binaryStr)
		for i in range(siz):
			binaryStr = paddedStr + binaryStr
		return binaryStr


class Solution:

	def invertBits(self, bin):
		xor = (1 << 32) - 1
		return bin ^ xor


class Solution:

	def invertBits(self, bin):
		for i in range(32):
			bin = bin ^ 1 << i
		return bin