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

57,007 answers

573 users

How to XOR byte arrays in Rust

1 Answer

0 votes
fn xor_bytes(a: &[u8], b: &[u8]) -> Vec<u8> {
    a.iter().zip(b.iter()).map(|(x, y)| x ^ y).collect()
}

fn print_bitset_array(label: &str, array: &[u8]) {
    print!("{}: ", label);
    for byte in array {
        print!("{:08b} ", byte);
    }
    println!();
}

fn main() {
    let a = b"Aragorn";
    let b = b"Boromir";

    let c = xor_bytes(a, b);

    print_bitset_array("a", a);
    print_bitset_array("b", b);
    print_bitset_array("c", &c);

    print!("c: ");
    for byte in &c {
        print!("{} ", byte);
    }
    println!();

    println!("c as string: {:?}", String::from_utf8_lossy(&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: "\u{3}\u{1d}\u{13}\u{8}\u{2}\u{1b}\u{1c}"

*/
 

 



answered Jul 12, 2025 by avibootz

Related questions

1 answer 175 views
175 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 191 views
191 views asked Jul 12, 2025 by avibootz
1 answer 204 views
...