英文:
golang: bitwise operation on very long binary bit string representation
问题
作为练习,我得到了两个非常大的字符串,其中包含长的二进制表示。这里是一个简短的示例,但可能有超过100位:
示例:
11100
00011
输出为按位或(作为字符串):
11111
我的方法是解析每个字符串的字符,进行按位或操作,并构建一个新的字符串,但对于大型输入来说处理时间太长且不够有效。
然后,ParseInt方法限制为64位长度:
num1, err := strconv.ParseInt("11100", 2, 64)
num2, err := strconv.ParseInt("00011", 2, 64)
res := num1 | num2
如何处理两个字符串二进制表示之间的按位或操作?
英文:
As an exercise, in input I got 2 very big string containing long binary representation here a short one but could have more than 100 bits:
Example
11100
00011
With output in bitwise OR (as string)
11111
My approach was to parse each string characters and make a bitwise OR and build a new string but it is too long to process on big entry and not effective.
Then ParseInt method is restricted to a 64 bit length
num1, err:= strconv.ParseInt("11100", 2, 64)
num2, err:= strconv.ParseInt("00011", 2, 64)
res := num1 | num2
How to deal with a bitwise OR between 2 string binary representation?
答案1
得分: 9
你可以通过字符比较来创建结果的按位或字符串,或者可以使用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()
}
英文:
You could create the resulting bitwise OR string by doing character comparisons, or you can perform arbitrary large numeric operations using math/big
. Here is an example of such an operation:
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()
}
答案2
得分: 0
虽然你可以将这些字符串转换为数字来执行位运算,但如果你的目标只是对这两个字符串执行一次位或运算,那么将字符串解析为数字将比仅仅迭代字符串以实现最终结果的方法效率低下。只有在以二进制形式对数字执行大量操作时,这样做才有意义。
下面是在下面的字符串上执行OR操作的示例代码。请注意,此代码假设字符串与问题中的示例长度相同,如果它们的长度不同,您还需要处理这个问题。
package main
import "fmt"
func main() {
n1 := "1100"
n2 := "0011"
fmt.Printf("Input: %v | %v\n", n1, n2)
if len(n1) != len(n2) {
fmt.Println("Only supports strings of the same length")
return
}
result := make([]byte, len(n1))
for i := 0; i < len(n1); i++ {
switch n1[i] {
case '0':
switch n2[i] {
case '0':
result[i] = '0'
case '1':
result[i] = '1'
}
case '1':
switch n2[i] {
case '0':
result[i] = '1'
case '1':
result[i] = '1'
}
}
}
fmt.Println("Result: ", string(result))
}
你可以在这里运行这段代码:http://play.golang.org/p/L3o6_jYdi1
英文:
While you could convert these to numbers to perform bitwise operations, if your only goal is to perform a single bitwise OR on the two strings, parsing the strings into numbers will be less efficient than simply iterating over the string to achieve your end result. Doing so would only make sense if you were performing lots of operations on the numbers in their binary form.
Example code for performing an OR operation on the strings below. Do note that this code assumes the strings are the same length as the examples in the question are, if they were of different lengths you would need to handle that as well.
package main
import "fmt"
func main() {
n1 := "1100"
n2 := "0011"
fmt.Printf("Input: %v | %v\n", n1, n2)
if len(n1) != len(n2) {
fmt.Println("Only supports strings of the same length")
return
}
result := make([]byte, len(n1))
for i := 0; i < len(n1); i++ {
switch n1[i] {
case '0':
switch n2[i] {
case '0':
result[i] = '0'
case '1':
result[i] = '1'
}
case '1':
switch n2[i] {
case '0':
result[i] = '1'
case '1':
result[i] = '1'
}
}
}
fmt.Println("Result: ", string(result))
}
答案3
得分: 0
这是一个Go语言的代码片段,它的功能是将两个二进制字符串进行比较,并将它们的对应位进行逻辑运算后输出结果。
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))
}
这段代码的作用是比较两个二进制字符串 a
和 b
,并将它们的对应位进行逻辑运算后输出结果。你可以在 goplayground 上运行这段代码。
英文:
How about this:
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))
}
答案4
得分: 0
尝试使用这个库:https://github.com/aristofanio/bitwiser。
你可以像处理bitstring一样解析大型字节数组。示例代码如下:
package main
import (
"github.com/aristofanio/bitwiser"
)
func main() {
//
b0, _ := bitwiser.ParseFromBits("011100")
b1, _ := bitwiser.ParseFromBits("11010011100")
//
println(b0.ToString()) //输出: 0x1c (数组长度为1字节)
println(b1.ToString()) //输出: 0x069c (数组长度为2字节)
}
英文:
Alternative: try this library:https://github.com/aristofanio/bitwiser.
you can parse large bytes arrays like bitstring. See:
package main
import (
"github.com/aristofanio/bitwiser"
)
func main() {
//
b0, _ := bitwiser.ParseFromBits("011100")
b1, _ := bitwiser.ParseFromBits("11010011100")
//
println(b0.ToString()) //output: 0x1c (len(array) = 1byte)
println(b1.ToString()) //output: 0x069c (len(array) = 2bytes)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论