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,695 questions

57,003 answers

573 users

How to XOR byte arrays in Swift

1 Answer

0 votes
import Foundation

func xorBytes(_ a: [UInt8], _ b: [UInt8]) -> [UInt8] {
    return zip(a, b).map { $0 ^ $1 }
}

func printBitsetArray(label: String, array: [UInt8]) {
    print("\(label): ", terminator: "")
    for byte in array {
        let bits = String(byte, radix: 2).padding(toLength: 8, withPad: "0", startingAt: 0)
        print(bits, terminator: " ")
    }
    print()
}

let a = Array("Aragorn".utf8)
let b = Array("Boromir".utf8)
let c = xorBytes(a, b)

printBitsetArray(label: "a", array: a)
printBitsetArray(label: "b", array: b)
printBitsetArray(label: "c", array: c)

print("c: ", terminator: "")
for byte in c {
    print(byte, terminator: " ")
}



/*
run:

a: 10000010 11100100 11000010 11001110 11011110 11100100 11011100 
b: 10000100 11011110 11100100 11011110 11011010 11010010 11100100 
c: 11000000 11101000 10011000 10000000 10000000 11011000 11100000 
c: 3 29 19 8 2 27 28 

*/

 



answered Jul 12, 2025 by avibootz

Related questions

1 answer 164 views
164 views asked Jul 11, 2025 by avibootz
1 answer 182 views
1 answer 169 views
169 views asked Jul 12, 2025 by avibootz
1 answer 176 views
176 views asked Jul 12, 2025 by avibootz
1 answer 191 views
191 views asked Jul 12, 2025 by avibootz
...