How to add Two Binary Strings in Golang?

This tutorial will show how we can add two binary strings in Golang. Also, we will cover and understand all the cases while adding two strings using examples.

Example

• Suppose we want to add these two binary strings “111” and “1011” whose numeric values are 7 and 11 whose addition is 18 with binary representation “10010”. Now we will do the addition of these strings step by step below.variables with the respective values you want to multiply.

• As you can see the length of string “111” is less than “1011” so we have to make them equal for which we can add “0” in a string that is shorter in length due to which the value will also remain the same. The length of “111” is shorter than “1011” so we will add one zero.

• Now strings look like this,

“0111” and “1011”

Algorithm

• STEP 1 − Declaring the strings which are going to add.

• STEP 2 − Initializing the strings with the binary strings.

• STEP 3 − Adding ‘0’ in starting to the smaller string.

• STEP 4 − Adding both the strings and storing them in the third string.

Example

package main

// fmt package provides the function to print anything
import "fmt"

// function which will add the binary strings
func binaryAdditionOfStrings(string1, string2 string) string {
// checking if the length of the first string is greater then
// second then calling the function by swapping the parameters
if len(string1) > len(string2) {
}
// finding the difference between the length of the strings
difference := len(string2) - len(string1)

// making both strings equal by adding 0 in front of a smaller string
for i := 0; i < difference; i++ {
string1 = "0" + string1
}
// initializing a variable carry to keep the track of carry after

carry := "0"

// In this variable we will store our final string

// traversing the strings and adding them by picking the index from the end /*
/* For example, we are adding “100” and ”110”.
So, for the last characters in the string i.e “0” and “0” the first else
if condition will run.
Then for the middle characters i.e “0” and “1” the last else if
condition will run and
for the first characters i.e “1” and “1” the first if condition will run.
*/
for i := len(string1) - 1; i >= 0; i-- {
if string1[i] == '1' && string2[i] == '1' {
if carry == "1" {
} else {
carry = "1"
}
} else if string1[i] == '0' && string2[i] == '0' {
if carry == "1" {
carry = "0"
} else {
}
} else if string1[i] != string2[i] {
if carry == "1" {
} else {
}
}
}
if carry == "1" {
}
}
func main() {

// declaring the strings
var string1, string2 string

// initializing the strings
string1 = "10101"
string2 = "100111"

// Printing the result of the addition of both the binary strings
fmt.Println("The Numeric representation of", string1, "is", "21.")
fmt.Println("The Numeric representation of", string2, "is", "39.")

fmt.Println("The Binary addition of", string1, "and", string2, "is", result, "whose value in numeric is 60.")}


In the above code, the main logic resides in binaryAdditionOfStrings() function so we will discuss it line by line.

• if len(string1) > len(string2) {} - First, check if the length of the first string is greater than the second then call the function again by swapping the parameters.

• difference:= len(string2) - len(string1) - Next, we are finding the difference between the length of the strings and adding that number of zeros in the smaller one to make both strings equal in size.

• carry:= "0" - Now we are declaring the carry and answer variable and will update it while traversing the strings.

• This is the step that includes the core logic where we are running a for loop over the strings and traversing from the last.

• if string1[i] == '1' && string2[i] == '1' {} - The first if the condition is checking the current index value in both the strings is “1” if yes then we are checking the value of carrying

• If carry is “1” then we have to add three “1” which is equal to “11” in binary(3 in numeric) so then we will add “1” in front of the answer string and carry will remain “1”

• Else we have to just add two “1” which is equal to “10” in binary(2 in numeric) so then we will add “0” in the front of the answer string and set carry “1”.

• else if string1[i] == '0' && string2[i] == '0' {} - Now the else if the condition is checking that the value at the current index in both arrays is zero then we are checking the carry.

• If the carry is “1” then we have to add one “1” and two “0” which is equal to “1” in binary(1 in numeric) so then we will add “1” in front of the answer string and carry will become “0”

• Else we have to just add three “0” which is equal to “0” in binary(0 in numeric) so then we will add “0” in the front of the answer string and carry will remain “0”.

• else if string1[i] != string2[i] - In the last condition if any one of the values in both the string at the current index is “1” and another one is “0” then we are checking the value of carrying

• If carry is “1” then we have to add two “1” which is equal to “10” in binary(2 in numeric) so then we will add “0” in front of the answer string and carry will remain “1”.

• Else we have to just add one “1” which is equal to “1” in binary(1 in numeric) so then we will add “1” in the front of the answer string and the carry will remain “0”.

• Once the above loop ends then we are checking that the carry is “1” or not. If yes then we are adding it to the end of the answer string and returning it.

Output

The numeric representation of 10101 is 21.
The numeric representation of 100111 is 39.
The binary addition of 10101 and 100111 is 111100 whose value in numeric is 60.