Given two integer numbers, F and S. In binary form of the numbers, for every i^{th }set bit in F, negate the i ^{th }bit in S.
Example 1:
Input:
F = 2 , S = 5
Output:
7
Explanation:
2 is represented as 10 in binary and
5 is represented as 101. Hence negating
the 2nd bit of 5 from right, thus the
new number becomes 7 i.e. 111. 
Example 2:
Input:
F = 1 , S = 3
Output:
2
Explanation:
1-1 and 3-11 in binary, thus negating
1st bit of 3 from right we get 10 i.e. 2. 
 
Your Task:
You don't need to read input or print anything. Your task is to complete the function negaBit() which takes 2 Integers F and S as input and returns the modified S.
Expected Time Complexity: O(1)
Expected Auxiliary Space: O(1)
 
Constraints:
1 <= F,S <= 10^{6}

Source: geeksforgeeks | Difficulty: EASY
Tags: Bit manipulation

Mathematics

Starter code: #User function Template for python3

class Solution:
    def negaBit(self, F , S):
        # code here 

Solutions:
class Solution:

	def negaBit(self, F, S):
		return F ^ S


class Solution:

	def negaBit(self, F, S):
		bin1 = bin(F)[2:]
		bin2 = bin(S)[2:]
		max_len = max(len(bin1), len(bin2))
		bin1 = str(bin1.zfill(max_len))
		bin2 = str(bin2.zfill(max_len))
		for i in range(len(str(bin1))):
			if bin1[i] == '1':
				if bin2[i] == '1':
					bin2 = bin2[:i] + '0' + bin2[i + 1:]
				else:
					bin2 = bin2[:i] + '1' + bin2[i + 1:]
		return int(bin2, 2)


class Solution:

	def negaBit(self, f, s):
		return f ^ s


class Solution:

	def negaBit(self, F, S):
		if F < S:
			y = bin(F)[2:]
			z = bin(S)[2:]
			new_bin = bin(S)[0:len(z) - len(y) + 2]
		else:
			y = bin(S)[2:]
			z = bin(F)[2:]
			new_bin = bin(F)[0:len(z) - len(y) + 2]
		bit_index = 0
		new_binary_1 = ''
		while bit_index < len(y):
			new_binary_1 = str(abs(int(y[len(y) - bit_index - 1]) - int(z[len(z) - 1 - bit_index]))) + new_binary_1
			bit_index += 1
		new_bin = new_bin + new_binary_1
		i = 2
		decimal_1 = 0
		while i < len(new_bin):
			r = int(new_bin[i]) * 2 ** (len(new_bin) - i - 1)
			decimal_1 = decimal_1 + r
			i += 1
		return decimal_1


class Solution:

	def negaBit(self, F, S):
		bin_f = str(bin(F)).replace('0b', '')
		bin_s = str(bin(S)).replace('0b', '')
		sets = []
		if len(bin_f) > len(bin_s):
			bin_s = '0' * (len(bin_f) - len(bin_s)) + bin_s
		elif len(bin_f) < len(bin_s):
			bin_f = '0' * (len(bin_s) - len(bin_f)) + bin_f
		for i in range(-1, -len(bin_f) - 1, -1):
			if bin_f[i] == '1':
				sets.append(i)
		for i in sets:
			set = i
			val = len(bin_s) - -set
			if bin_s[set] == '1':
				bin_s = bin_s[0:val] + '0' + bin_s[val + 1:len(bin_s)]
			elif bin_s[set] == '0':
				bin_s = bin_s[0:val] + '1' + bin_s[val + 1:len(bin_s)]
		return int(bin_s, 2)


class Solution:

	def negaBit(self, F, S):
		F = bin(F)[2:]
		S = bin(S)[2:]
		diff = len(F) - len(S)
		if diff > 0:
			S = '0' * diff + S
		elif diff < 0:
			F = '0' * -diff + F
		S = list(S)
		for i in range(len(F) - 1, -1, -1):
			j = len(S) - 1 - (len(F) - 1 - i)
			if F[i] == '1' and 0 <= j < len(S):
				S[j] = '1' if S[j] == '0' else '0'
		return int(''.join(S), 2)