Skip to content

Welcome to Tech by Example

Menu
  • Home
  • Posts
  • System Design Questions
Menu

Add two binary numbers program

Posted on January 6, 2022January 6, 2022 by admin

Overview

The objective is to add two given binary numbers. Binary numbers are only composed of digits 0 and 1. Below is the binary addition logic for individual digits

  • 0+0 = Sum is 0, Carry is 0
  • 0+1 = Sum is 1, Carry is 0
  • 1+0 = Sum is 0, Carry is 0
  • 1+1 = Sum is 0, Carry is 1
  • 1+1+1 = Sum is 1, Carry is 1

Examples

Input: "101" + "11"
Output: "1000"

Input: "111" + "101"
Output: "1100"

Program

Below is the program for the same

package main

import (
	"fmt"
	"strconv"
)

func addBinary(a string, b string) string {
	lenA := len(a)
	lenB := len(b)

	i := lenA - 1
	j := lenB - 1

	var output string
	var sum int
	carry := 0
	for i >= 0 && j >= 0 {
		first := int(a[i] - '0')
		second := int(b[j] - '0')

		sum, carry = binarySum(first, second, carry)

		output = strconv.Itoa(sum) + output
		i = i - 1
		j = j - 1
	}

	for i >= 0 {
		first := int(a[i] - '0')

		sum, carry = binarySum(first, 0, carry)

		output = strconv.Itoa(sum) + output
		i = i - 1

	}

	for j >= 0 {
		second := int(b[j] - '0')

		sum, carry = binarySum(0, second, carry)

		output = strconv.Itoa(sum) + output
		j = j - 1
	}

	if carry > 0 {
		output = strconv.Itoa(1) + output
	}

	return output
}

func binarySum(a, b, carry int) (int, int) {
	output := a + b + carry

	if output == 0 {
		return 0, 0
	}

	if output == 1 {
		return 1, 0
	}

	if output == 2 {
		return 0, 1
	}

	if output == 3 {
		return 1, 1
	}

	return 0, 0
}

func main() {
	output := addBinary("101", "11")
	fmt.Println(output)

	output = addBinary("111", "101")
	fmt.Println(output)
}

Output

1000
1100
©2025 Welcome to Tech by Example | Design: Newspaperly WordPress Theme