Golang 如何添加两个二进制字符串
本教程将展示我们如何在Golang中添加两个二进制字符串。同时,我们将通过示例涵盖并理解在添加两个字符串时的所有情况。
示例
- 假设我们想要将这两个二进制字符串“111”和“1011”相加,它们的数值分别为7和11,它们的相加结果为18,二进制表示为“10010”。现在我们将逐步进行这些字符串的相加,如下所示。
-
正如你所看到的,字符串“111”的长度小于“1011”,所以我们必须使它们相等,我们可以在长度较短的字符串中添加“0”,从而保持值不变。”111″的长度小于”1011″,所以我们将添加一个零。
-
现在字符串看起来像这样:
“0111” and “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() 函数中,因此我们将逐行讨论它。
- 如果
len(string1) > len(string2) {}
– 首先,检查第一个字符串的长度是否大于第二个字符串的长度,然后通过交换参数再次调用函数。 -
difference:= len(string2) - len(string1)
– 接下来,我们找到字符串长度的差异,并在较小的字符串中添加相应数量的零,使得两个字符串的大小相等。 -
carry:= "0"
– 现在,我们声明了进位和答案变量,并在遍历字符串时进行更新。 -
这一步包含了核心逻辑,我们在字符串上运行一个 for 循环,并从最后一个位置开始遍历。
if string1[i] == '1' && string2[i] == '1' {}
– 第一个 if 条件是检查两个字符串中当前索引位置的值是否都为 “1”,如果是,则检查进位的值- 如果进位是“1”,那么我们需要添加三个“1”,即二进制中的“11”(数值为3),然后我们在答案字符串前面添加“1”,进位仍然为“1”
-
否则,我们只需要添加两个“1”,即二进制中的“10”(数值为2),然后我们在答案字符串前面添加“0”,设置进位为“1”。
-
else if string1[i] == '0' && string2[i] == '0' {}
– 现在,else if条件检查两个数组中当前索引的值是否为零,然后我们检查进位。- 如果进位是“1”,那么我们需要添加一个“1”和两个“0”,即二进制中的“1”(数值为1),然后我们在答案字符串前面添加“1”,进位将变为“0”
-
否则,我们需要添加三个“0”,这相当于二进制表示中的“0”(数字中的0),然后我们将在答案字符串前面添加“0”,进位将保持为“0”。
-
否则,如果
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中添加两个二进制字符串及其代码的全部内容。要了解更多关于Golang的知识,您可以探索这里的教程。