Golang 如何添加两个二进制字符串

Golang 如何添加两个二进制字符串

本教程将展示我们如何在Golang中添加两个二进制字符串。同时,我们将通过实例介绍并理解添加两个字符串的所有情况。

例子

  • 假设我们想把两个二进制字符串 “111 “和 “1011 “相加,这两个字符串的数值是7和11,它们的加数是18,二进制表示为 “10010”。现在我们将在下面一步一步地做这些字符串的加法。变量与你要乘的各自数值相加。

  • 正如你所看到的,字符串 “111 “的长度小于 “1011”,所以我们必须使它们相等,为此我们可以在长度较短的字符串中加入 “0”,由于这样,数值也将保持不变。111 “的长度比 “1011 “短,所以我们将添加一个0。

  • 现在字符串看起来像这样。

“0111” 和 “1011”

算法

  • 第1步 – 声明要添加的字符串。

  • 第2步 – 用二进制字符串初始化这些字符串。

  • 第3步 – 将’0’添加到较小的字符串中。

  • 第4步 – 将两个字符串相加,并将其存储在第三个字符串中。

例子

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) {
      return binaryAdditionOfStrings(string2, string1)
   }
   // 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

   // each addition
   carry := "0"

   // In this variable we will store our final string
   answer := ""

   // 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" {
            answer = "1" + answer
         } else {
            answer = "0" + answer
            carry = "1"
         }
      } else if string1[i] == '0' && string2[i] == '0' {
         if carry == "1" {
            answer = "1" + answer
            carry = "0"
         } else {
            answer = "0" + answer
         }
      } else if string1[i] != string2[i] {
         if carry == "1" {
            answer = "0" + answer
         } else {
            answer = "1" + answer
         }
      }
   }
   if carry == "1" {
      answer = "1" + answer
   }
   return answer
}
func main() {

   // declaring the strings
   var string1, string2 string

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

   result := binaryAdditionOfStrings(string1, string2)
   // 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.")}

在上面的代码中,主要的逻辑存在于 binaryAdditionOfStrings() 函数中,所以我们将逐行讨论它。

  • if len(string1) > len(string2) {} – 首先,检查第一个字符串的长度是否大于第二个,然后通过交换参数再次调用该函数。

  • difference:= len(string2) – len(string1) – 接下来,我们要找出两个字符串的长度之差,并在较小的那个字符串中加上这个数字的零,使两个字符串的大小相等。

  • carry:= “0” – 现在我们正在声明carry和answer变量,并在遍历字符串时更新它。

  • 这一步包括核心逻辑,我们在字符串上运行一个for循环,并从最后一个字符串开始遍历。

    • if string1[i] == ‘1’ && string2[i] == ‘1’ {} – 第一个条件是检查两个字符串中的当前索引值是否为 “1”,如果是,我们将检查携带值。
      • 如果携带值是 “1”,那么我们必须添加三个 “1”,这相当于二进制中的 “11”(数字中的3),因此我们将在答案字符串前面添加 “1”,携带值将保持 “1”

      • 否则我们只需添加两个 “1”,相当于二进制中的 “10”(数字中的2),所以我们将在答案字符串的前面添加 “0”,并设置携带量为 “1”。

    • else if string1[i] == ‘0’ && string2[i] == ‘0’ {} – 现在的else if条件是检查两个数组中当前索引的值是否为0,然后我们要检查携带量。

      • 如果进位是 “1”,那么我们必须添加一个 “1 “和两个 “0”,这相当于二进制中的 “1”(数字中的1),所以我们将在答案字符串前面添加 “1”,进位将变成 “0”

      • 否则,我们只需添加三个 “0”,这相当于二进制中的 “0”(数字中的 “0”),所以我们将在答案字符串的前面添加 “0”,携带量将保持 “0”。

    • else if string1[i] != string2[i] – 在最后一个条件中,如果当前索引的两个字符串中的任何一个值是 “1”,另一个是 “0”,那么我们将检查携带的值。

      • 如果携带值是 “1”,那么我们必须添加两个 “1”,这相当于二进制中的 “10”(数字中的2),因此我们将在答案字符串的前面添加 “0”,携带值将保持为 “1”。

      • 否则,我们只需添加一个 “1”,等于二进制中的 “1”(数字中的1),所以我们将在答案字符串前面添加 “1”,而携带量将保持为 “0”。

  • 一旦上述循环结束,我们将检查进位是否为 “1”。如果是的话,我们将把它添加到答案字符串的末尾并返回。

输出

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.

这都是关于在Golang中添加两个二进制字符串和它们的代码。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程