3 回答
TA贡献1839条经验 获得超15个赞
您可以通过进行字符比较来创建结果按位 OR 字符串,或者您可以使用math/big. 以下是此类操作的示例:
package main
import "fmt"
import "math/big"
func main() {
num1 := "11100"
num2 := "00011"
var bigNum1 big.Int
var bigNum2 big.Int
var result big.Int
if _, ok := bigNum1.SetString(num1, 2); !ok {
panic("invalid num1")
}
if _, ok := bigNum2.SetString(num2, 2); !ok {
panic("invalid num2")
}
result.Or(&bigNum1, &bigNum2)
for i := result.BitLen() - 1; i >= 0; i-- {
fmt.Print(result.Bit(i))
}
fmt.Println()
}
TA贡献1798条经验 获得超7个赞
这个怎么样:
package main
import "fmt"
func main(){
a := "01111100"
b := "1001000110"
var longest, len_diff int
if len(a) > len(b) {
longest = len(a)
len_diff = len(a) - len(b)
} else {
longest = len(b)
len_diff = len(b) - len(a)
}
temp_slice := make([] byte, longest)
var a_start, b_start int
if len(a) > len(b) {
for i := 0; i < len_diff; i++ {
temp_slice[i] = a[i]
}
a_start = len_diff
} else {
for i := 0; i < len_diff; i++ {
temp_slice[i] = b[i]
}
b_start = len_diff
}
for i := 0; i < (longest - len_diff); i++ {
if a[a_start + i] == '1' || b[b_start + i] == '1' {
temp_slice[len_diff + i] = '1'
} else {
temp_slice[len_diff + i] = '0'
}
}
fmt.Println(string(temp_slice))
}
- 3 回答
- 0 关注
- 327 浏览
添加回答
举报