'golang: bitwise operation on very long binary bit string representation

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?



Solution 1:[1]

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))
}

http://play.golang.org/p/L3o6_jYdi1

Solution 2:[2]

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))
}

goplayground

Solution 3:[3]

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)
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2
Solution 3