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

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

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,696 questions

55,455 answers

573 users

How to XOR byte arrays in Kotlin

1 Answer

0 votes
fun xorBytes(a: ByteArray, b: ByteArray): ByteArray {
    return ByteArray(a.size) { i -> (a[i].toInt() xor b[i].toInt()).toByte() }
}

fun printBitsetArray(label: String, array: ByteArray) {
    print("$label: ")
    array.forEach { byte ->
        print(byte.toUByte().toString(2).padStart(8, '0') + " ")
    }
    println()
}

fun main() {
    val a = "Aragorn".toByteArray(Charsets.US_ASCII)
    val b = "Boromir".toByteArray(Charsets.US_ASCII)
    val c = xorBytes(a, b)

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

    print("c: ")
    c.forEach { byte ->
        print("${byte.toUByte()} ")
    }
    println()
}

 
  
/*
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 

*/

 



answered Jul 12, 2025 by avibootz

Related questions

1 answer 106 views
1 answer 113 views
113 views asked Jul 12, 2025 by avibootz
1 answer 117 views
117 views asked Jul 12, 2025 by avibootz
1 answer 116 views
116 views asked Jul 12, 2025 by avibootz
1 answer 130 views
130 views asked Jul 12, 2025 by avibootz
1 answer 136 views
...