Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Disclosure: My content contains affiliate links.

43,700 questions

57,013 answers

573 users

How to XOR byte arrays in Go

1 Answer

0 votes
package main

import (
    "fmt"
)

func xorBytes(a, b []byte) []byte {
    result := make([]byte, len(a))
    for i := range a {
        result[i] = a[i] ^ b[i]
    }
    return result
}

func printBitsetArray(label string, array []byte) {
    fmt.Print(label + ": ")
    for _, b := range array {
        fmt.Printf("%08b ", b)
    }
    fmt.Println()
}

func main() {
    a := []byte("Aragorn")
    b := []byte("Boromir")

    c := xorBytes(a, b)

    printBitsetArray("a", a)
    printBitsetArray("b", b)
    printBitsetArray("c", c)

    fmt.Print("c: ")
    for _, b := range c {
        fmt.Printf("%d ", b)
    }
    fmt.Println()

    fmt.Printf("c as string: %q\n", string(c))
}



/*
run:

a: 01000001 01110010 01100001 01100111 01101111 01110010 01101110 
b: 01000010 01101111 01110010 01101111 01101101 01101001 01110010 
c: 00000011 00011101 00010011 00001000 00000010 00011011 00011100 
c: 3 29 19 8 2 27 28 
c as string: "\x03\x1d\x13\b\x02\x1b\x1c"

*/

 



answered Jul 12, 2025 by avibootz

Related questions

2 answers 220 views
220 views asked Jul 12, 2025 by avibootz
1 answer 165 views
165 views asked Jul 11, 2025 by avibootz
1 answer 183 views
183 views asked Jul 12, 2025 by avibootz
1 answer 182 views
1 answer 169 views
169 views asked Jul 12, 2025 by avibootz
1 answer 177 views
177 views asked Jul 12, 2025 by avibootz
...