Given two integers a and b. Find the sum of two numbers without using arithmetic operators.
Example 1:
Input:
a = 5, b = 3
Output: 8
Explanation :
5 + 3 = 8
Example 2:
Input:
a = 10, b = 30
Output: 40
Explanation:
10 + 30 = 40
Your task:
You don't need to read input or print anything. Your task is to complete the function sum() which takes two integers a and b as input and returns the sum of a and b, which is calculated without using any arithmetic operator.
 
Expected Time Complexity : O(max(number of bits in 'a', number of bits in 'b'))
Expected Auxiliary Space : O(1)
 
Constraints:
1<=a, b<=10^8

Source: geeksforgeeks | Difficulty: EASY
Tags: Bit manipulation

Data structures

Starter code: #User function Template for python3



def sum(a,b):

    #code here

Solutions:
def sum(a, b):
	while b != 0:
		c = a & b
		a = a ^ b
		b = c << 1
	return a


def sum(a, b):
	return a + b


def sum(a, b):
	return 2 * (a & b) + (a ^ b)


def sum(a, b):
	mask = 4294967295
	while b & mask > 0:
		carry = (a & b) << 1
		a = a ^ b
		b = carry
	return a & mask if b > 0 else a


def sum(a, b):
	while b != 0:
		tmp = (a & b) << 1
		a = a ^ b
		b = tmp
	return a


def sum(a, b):
	carry = b
	while carry != 0:
		sum = a ^ b
		carry = (a & b) << 1
		a = sum
		b = carry
	return sum


def sum(a, b):
	c = a + b
	return c


def sum(a, b):
	if b == 0:
		return a
	else:
		return sum(a ^ b, (a & b) << 1)


def sum(a, b):
	total = 0
	i = 0
	carry = 0
	if b > a:
		s = b
		b = a
		a = s
	while b != 0:
		carry = a & b
		a = a ^ b
		b = carry << 1
	return a


def sum(a, b):
	k = 0
	k = a + b
	return k


def sum(a, b):
	num1 = bin(a)[2:]
	num2 = bin(b)[2:]
	flag = 0
	output = ''
	while num1 and num2:
		if num1[-1] == '1' and num2[-1] == '1':
			output += str(flag)
			flag = 1
		elif num1[-1] == '0' and num2[-1] == '0':
			output += str(flag)
			flag = 0
		elif flag:
			output += '0'
		else:
			output += '1'
		num1 = num1[:-1]
		num2 = num2[:-1]
	num = num1 + num2
	while flag and num and (num[-1] == '1'):
		output += '0'
		num = num[:-1]
	if flag:
		output += '1'
		num = num[:-1]
	return int(num + output[::-1], 2)


def sum(a, b):
	while b != 0:
		carr = a & b
		a = a ^ b
		b = carr << 1
	return a


def sum(a, b):
	while b != 0:
		hld = a & b
		a = a ^ b
		b = hld << 1
	return a


def sum(a, b):
	s = a ^ b
	c = (a & b) << 1
	return s + c


import math

def sum(a, b):
	return a + b


def sum(a, b):
	add = 0
	carry = 0
	power = 0
	while a != 0 and b != 0:
		if carry == 0:
			if a % 2 != 0 and b % 2 == 0 or (a % 2 == 0 and b % 2 != 0):
				add += 2 ** power
			elif a % 2 != 0 and b % 2 != 0:
				carry = 1
		elif a % 2 == 0 and b % 2 == 0:
			add += 2 ** power
			carry = 0
		elif a % 2 != 0 and b % 2 == 0 or (a % 2 == 0 and b % 2 != 0):
			carry = 1
		else:
			add += 2 ** power
			carry = 1
		a = int(a / 2)
		b = int(b / 2)
		power += 1
	while a != 0:
		if carry == 0:
			if a % 2 != 0:
				add += 2 ** power
		elif a % 2 == 0:
			carry = 0
			add += 2 ** power
		else:
			carry = 1
		a = int(a / 2)
		power += 1
	while b != 0:
		if carry == 0:
			if b % 2 != 0:
				add += 2 ** power
		elif b % 2 == 0:
			carry = 0
			add += 2 ** power
		else:
			carry = 1
		b = int(b / 2)
		power += 1
	if carry == 1:
		add += 2 ** power
	return add


def sum(a, b):
	while b:
		s = a ^ b
		carry = a & b
		carry = carry << 1
		a = s
		b = carry
	return a


t = sum

def sum(a, b):
	return t([a, b])


def sum(x, y):
	if y == 0:
		return x
	else:
		return sum(x ^ y, (x & y) << 1)


def sum(a, b):
	s = a ^ b
	carry = a & b
	if carry == 0:
		return s
	else:
		return sum(s, carry << 1)


def sum(a, b):
	add = a + b
	return add


def sum(byuti, singh):
	byuti += singh
	return byuti


def sum(a, b):
	one = bin(a)[2:]
	sec = bin(b)[2:]
	while len(one) < len(sec):
		one = '0' + one
	while len(sec) < len(one):
		sec = '0' + sec
	ans = ''
	i = len(one) - 1
	carry = '0'
	while i >= 0:
		if one[i] == '1' and sec[i] == '1' and (carry == '1'):
			ans = '1' + ans
		elif one[i] == '1' and sec[i] == '1' and (carry == '0'):
			ans = '0' + ans
			carry = '1'
		elif one[i] == '1' or sec[i] == '1':
			if carry == '1':
				ans = '0' + ans
			else:
				ans = '1' + ans
		elif one[i] == '0' and sec[i] == '0':
			if carry == '1':
				ans = '1' + ans
				carry = '0'
			else:
				ans = '0' + ans
		i -= 1
	if carry == '1':
		ans = '1' + ans
	return int(ans, 2)


def sum1(a, b):
	return sum([a, b])


def sum(a, b):
	return a + b
sum(5, 3)


def sum(a, b):
	return a - ~b - 1


from numpy import add

def sum(a, b):
	return add(a, b)


def sum(a, b):
	summ = a ^ b
	carry = (a & b) << 1
	while carry:
		temp = summ ^ carry
		carry = (summ & carry) << 1
		summ = temp
	return summ