Implement a function that adds two numbers together and returns their sum in binary. The conversion can be done before, or after the addition.

The binary number returned should be a string.

Source: codewars | Difficulty: EASY
Tags: Bit manipulation

Fundamentals

Starter code: def add_binary(a,b):
	

Solutions:
def add_binary(a, b):
	return bin(a + b)[2:]


def add_binary(a, b):
	return '{0:b}'.format(a + b)


def find_highest_power_2(num):
	n = 0
	while 2 ** n <= num:
		n += 1
	return n - 1

def add_binary(a, b):
	sum = a + b
	number = 0
	while sum != 0:
		place_holder = find_highest_power_2(sum)
		number += 10 ** place_holder
		sum = sum - 2 ** place_holder
	return str(number)


def add_binary(a, b):
	c = a + b
	return bin(c).replace('0b', '')


def add_binary(a, b):
	return convert_to_binary(a + b)[::-1]

def convert_to_binary(num):
	if num == 0:
		return '1'
	elif num == 1:
		return '1'
	elif num % 2 == 0:
		return '0' + convert_to_binary(num / 2)
	else:
		return '1' + convert_to_binary(num - 1)[1:]


def add_binary(a, b):
	return f'{a + b:b}'


def add_binary(a, b):
	n = a + b
	binList = []
	while n > 0:
		binList.append(n % 2)
		n = n // 2
	return ''.join(map(str, reversed(binList)))


def add_binary(a, b):
	if a + b >= 0:
		return bin(a + b)[2:]
	else:
		return '-' + bin(a + b)[3:]


add_binary = lambda a, b: bin(a + b)[2:]


def add_binary(a, b):
	return str(bin(a + b)).split('b')[1]


def add_binary(n1, n2):
	return f'{n1 + n2:b}'


def add_binary(*a):
	return '{:b}'.format(sum(a))


def add_binary(a, b):
	return format(a + b, 'b')
print(add_binary(1, 1))


def add_binary(a, b):
	c = a + b
	l = []
	if c < 0:
		return '-' + dec2bin(abs(c))
	while True:
		(c, remainder) = divmod(c, 2)
		l.append(str(remainder))
		if c == 0:
			return ''.join(l[::-1])


def add_binary(a, b):
	ans = a + b
	result = ''
	while ans != 0:
		result += str(ans % 2)
		ans = ans // 2
	return result[::-1]


def add_binary(a, b):
	return toBinary(a + b)

def toBinary(n):
	result = ''
	while n > 1:
		result += str(n % 2)
		n = n // 2
	result += str(n % 2)
	return result[::-1]


def add_binary(a, b):
	ans = a + b
	return str(int(bin(ans)[2:]))


def add_binary(a, b):
	return '{number:b}'.format(number=a + b)


def add_binary(a: int, b: int) -> str:
	return format(a + b, 'b')


def add_binary(a, b):

	def translate_bin(dec):
		if dec == 0:
			return '0'
		elif dec == 1:
			return '1'
		else:
			return translate_bin(dec // 2) + str(dec % 2)
	return translate_bin(a + b)


def add_binary(a, b):
	res = a + b
	buf = ''
	while res >= 1:
		buf += str(res % 2)
		res = res // 2
	buf = buf[::-1]
	return buf


add_binary = lambda a, b: format(a + b, 'b')


def add_binary(a, b):
	r = a + b
	s = ''
	while r > 0:
		s += '1' if r % 2 == 1 else '0'
		r = r >> 1
	return s[::-1]


def add_binary(a, b):
	c = a + b
	output = []
	while c / 2 != 0:
		output.append(str(c % 2))
		c = c // 2
	output.reverse()
	return ''.join(output)


def add_binary(a, b):
	output = ''
	result = a + b
	bit = 1
	while bit <= result:
		output = ('1' if bit & result > 0 else '0') + output
		bit *= 2
	return output


def add_binary(a, b):
	sum = a + b
	divisors = list()
	for r in range(sum):
		x = 2 ** r
		if x > sum:
			break
		divisors.append(x)
	divisors = list(reversed(divisors))
	output = ''
	count = 0
	while sum > 0:
		if divisors[count] > sum:
			output = output + '0'
		else:
			output = output + '1'
			sum -= divisors[count]
			if sum == 0:
				output = output + '0' * (len(divisors) - count - 1)
		count += 1
	return output


def add_binary(a, b):
	c = a + b
	d = str(bin(c))
	f = d[2:]
	return f


def add_binary(a, b):
	return dec2bin(a + b)

def dec2bin(dec):
	bin_right = dec % 2
	dec_left = dec // 2
	if dec_left < 1:
		return str(bin_right)
	else:
		return str(dec2bin(dec_left)) + str(bin_right)


def add_binary(a, b):
	results_dec = a + b
	results_bin = bin(results_dec)
	results_bin = results_bin[2:len(results_bin)]
	return results_bin


def add_binary(a, b):
	tot = a + b
	i = 0
	binsum = ''
	while i < i + 1:
		if tot >= 2 ** i and tot < 2 ** (i + 1):
			binsum = binsum + '1'
			tot = tot - 2 ** i
			for j in range(i - 1, -1, -1):
				if tot < 2 ** j:
					binsum = binsum + '0'
				else:
					binsum = binsum + '1'
					tot = tot - 2 ** j
			break
		i += 1
	return binsum
print(add_binary(43, 11))


def add_binary(a, b):
	sum = a + b
	binario = bin(sum)
	return binario[2:]


def add_binary(a, b):
	fun = lambda x: fun(x // 2) + str(x % 2) if x >= 1 else ''
	return fun(a + b)


def add_binary(a, b):
	c = bin(a + b)
	bin_c = ''
	bin_lst = []
	for char in c:
		bin_lst.append(char)
	for num in bin_lst[2:len(bin_lst)]:
		bin_c += num
	return bin_c


def add_binary(a, b):
	c = bin(int(a) + int(b))
	c = str(c)
	c = list(c)
	return ''.join(c[2:len(c) + 1])


def add_binary(a, b):
	sum1 = int(a + b)
	i = 0
	str1 = ''
	if sum1 == 0:
		str1 = '0'
	while sum1 != 0:
		rem = int(sum1 % 2)
		str1 = str(rem) + str1
		sum1 = int(sum1 // 2)
	return str1


def add_binary(a, b):
	summ = a + b
	binary = ''
	while True:
		rem = summ % 2
		binary += str(rem)
		summ -= summ % 2
		summ //= 2
		if summ == 0:
			break
	return binary[::-1]


def add_binary(a, b):
	c = a + b
	binary = ''
	while c > 1:
		binary = binary + str(c % 2)
		c = c // 2
	binary = binary[::-1]
	binary = str(c) + binary
	return binary


def add_binary(a, b):
	total = a + b
	out_str = ''
	while total > 0:
		if total % 2 == 0:
			out_str = '0' + out_str
		else:
			out_str = '1' + out_str
		total = total // 2
	return out_str


add_binary = lambda _, __: bin(_ + __)[2:]


def add_binary(a, b):
	c = a + b
	bine = str(bin(c)[2:])
	return bine


def add_binary(a, b):
	string = bin(a + b)
	return string.replace('0b', '')


def add_binary(a, b):
	if type(a) == str and type(b) == str:
		res = bin(int(a, 2) + int(b, 2))
	else:
		res = bin(a + b)
	return res[2:]


def add_binary(a, b):
	num = a + b
	temp = bin(num)
	index = 0
	binary = ''
	for i in temp:
		if index != 0 and index != 1:
			binary += i
		index += 1
	return binary


def add_binary(a, b):
	itog = ''
	c = a + b
	while c != 1:
		itog = str(c % 2) + itog
		c = c // 2
	itog = '1' + itog
	return itog


import math

def add_binary(a, b):
	assert type(a) is int
	assert type(b) is int
	a = a + b
	exps = []
	while a != 0:
		b = int(math.floor(math.log(a, 2)))
		exps.append(b)
		if a == 1:
			break
		a = a % 2 ** b
	byte = ''.join((['0', '1'][x in exps] for x in range(exps[0], -1, -1)))
	return byte


def add_binary(a, b):
	c = a + b
	x = 0
	while 2 ** x <= c:
		x += 1
	if 2 ** x > c:
		x -= 1
	reverse = [i for i in range(x + 1)]
	reverse = reverse[::-1]
	print(reverse)
	res = ''
	for i in reverse:
		if 2 ** i <= c:
			c -= 2 ** i
			res += '1'
		else:
			res += '0'
	return res


def add_binary(a, b):
	count = a + b
	output = ''
	while True:
		output += str(count % 2)
		count = count // 2
		if count < 2:
			output += str(count)
			output = output[::-1]
			while output.startswith('0'):
				output = output[1:]
			return output


def add_binary(a, b):
	add = a + b
	return str(int(bin(add)[2:]))


def add_binary(a, b):
	suma = bin(a + b)
	return suma[2:len(suma)]


def add_binary(a, b):
	binaryNum = ''
	q = 0
	mod = 0
	cSum = a + b
	while cSum > 0:
		mod = cSum % 2
		cSum //= 2
		binaryNum += str(mod)
	return binaryNum[::-1]


def add_binary(a, b):
	n = a + b
	s = []
	while n != 0:
		(n, b) = divmod(n, 2)
		s.insert(0, b)
	return ''.join(map(str, s))


def add_binary(int1=0, int2=1):
	bin1 = bin(int1)
	bin2 = bin(int2)
	int_sum = int(bin1, 2) + int(bin2, 2)
	bin_sum = bin(int_sum)
	return bin_sum[2:]


import math

def add_binary(a, b):
	total = a + b
	bin = ''
	power = int(math.log(total, 2))
	runsum = 0
	while power >= 0:
		if 2 ** power + runsum <= total:
			bin += '1'
			runsum += 2 ** power
		else:
			bin += '0'
		power -= 1
	first_1 = bin.index('1')
	return bin[first_1:]


import math

def add_binary(a, b):
	sum = a + b
	return bin(sum)[2:]


def add_binary(a, b):
	v = a + b
	d = bin(v)[2:]
	c = str(d)
	return c


def add_binary(a, b):
	res = ''
	c = a + b
	while c > 1:
		rem = c % 2
		res += str(rem)
		c = c // 2
	res += str(1)
	return ''.join(list(reversed(res)))


def add_binary(a, b):
	x = a + b
	newstr = ''
	while x > 0:
		bin = x % 2
		newstr += str(bin)
		x = x // 2
	newstr = newstr[::-1]
	return newstr


def add_binary(a, b):
	sum = a + b
	res = ''
	while sum > 0:
		if sum & 1:
			res = '1' + res
		else:
			res = '0' + res
		sum = sum >> 1
	return res


def add_binary(a, b):
	out = a + b
	out = bin(out)
	out = out.split('b')
	return out[1]


def add_binary(a, b):
	Num = a + b
	BinString = str(bin(Num))[2:]
	return BinString


def add_binary(n1, n2):
	n = n1 + n2

	def decToBin(num):
		if num == 0:
			return ''
		else:
			return decToBin(num // 2) + str(num % 2)
	if n == 0:
		return 0
	else:
		return decToBin(n)


def add_binary(a, b):
	return convert_to_binary(a + b)

def convert_to_binary(a):
	remainders = []
	while a // 2 != 0:
		remainders.append(str(a % 2))
		a = a // 2
	return '1' + ''.join(remainders[::-1])


def add_binary(a, b):
	_bin = bin(a + b)
	return '{0:b}'.format(a + b)


def add_binary(a, b):
	s = a + b
	bins = [s % 2]
	while s > 1:
		s = s // 2
		bins.append(s % 2)
	final = ''
	for i in bins[::-1]:
		final += str(i)
	return final


def add_binary(a, b):
	num = a + b
	binary = ''
	while num > 0:
		val = num % 2
		binary += str(val)
		num = num // 2
	return binary[::-1]


def add_binary(a, b):
	if a == 0 and b == 0:
		return ''
	elif a & b & 1:
		return add_binary(a // 2 + 1, b // 2) + '0'
	elif (a | b) & 1:
		return add_binary(a // 2, b // 2) + '1'
	else:
		return add_binary(a // 2, b // 2) + '0'


def add_binary(a, b):
	sum = a + b
	binary_sum = ''
	while sum > 0:
		binary_sum = str(sum % 2) + binary_sum
		sum = sum // 2
	return binary_sum


def add_binary(a, b):
	somme = a + b
	binaire = ''
	i = 0
	while somme >= 2 ** (i + 1):
		i += 1
	while i >= 0:
		if somme >= 2 ** i:
			binaire = binaire + str(1)
			somme = somme - 2 ** i
		else:
			binaire = binaire + str(0)
		i -= 1
	return binaire


def add_binary(a, b):
	sum = int(a) + int(b)
	t = bin(sum)
	s = t[2:]
	return s


def add_binary(a, b):
	c = a + b
	x = ''
	while c != 0:
		z = c % 2
		x += str(z)
		c = c // 2
	return x[::-1]


def add_binary(a, b):
	z = a + b
	m = []
	while z != 0:
		m.append(z % 2)
		z = z // 2
	for i in m[::-1]:
		z = z * 10 + i
	return str(z)


def add_binary(a, b):
	sum = a + b
	msg = []
	y = sum
	while y > 1:
		y = y // 2
		msg.insert(0, str(y % 2))
	msg.append(str(sum % 2))
	return ''.join(msg)


def add_binary(a, b):
	s = a + b
	fin = []
	while s != 0:
		quo = s // 2
		rem = s % 2
		fin.append(rem)
		s = quo
	fin.reverse()
	return ''.join(map(str, fin))


def add_binary(a, b):
	suma = a + b
	temp = ''
	while suma > 0:
		temp = str(suma % 2) + temp
		suma = suma // 2
	return temp


def add_binary(a, b):
	decimal = a + b
	output = ''
	while decimal != 0:
		str_bin = str(decimal % 2)
		output = output + str_bin
		decimal = decimal // 2
	return output[::-1]


def add_binary(a, b):
	i = a + b
	result = []
	while i >= 2:
		result.append(str(i % 2))
		i //= 2
	result.append(str(i % 2))
	return ''.join(result[::-1])


def add_binary(a, b):
	suma = bin(a + b)
	suma = suma.split('0b')
	return str(suma[1])


def add_binary(a, b):
	sum = a + +b
	sum_bin = '{0:b}'.format(sum)
	return sum_bin
print(add_binary(5, 1))