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

55,449 answers

573 users

How to XOR byte arrays in JavaScript

1 Answer

0 votes
function xorBytes(a, b) {
    const result = new Uint8Array(a.length);
     
    for (let i = 0; i < a.length; i++) {
        result[i] = a[i] ^ b[i];
    }
     
    return result;
}
 
function printBitsetArray(label, array) {
    let output = label + ": ";
     
    for (let byte of array) {
        output += byte.toString(2).padStart(8, '0') + " ";
    }
     
    console.log(output);
}
 
// Initialize arrays using ASCII codes
const a = new Uint8Array([..."Aeryn"].map(ch => ch.charCodeAt(0)));
const b = new Uint8Array([..."Albus"].map(ch => ch.charCodeAt(0)));
 
const c = xorBytes(a, b);
 
printBitsetArray("a", a);
printBitsetArray("b", b);
printBitsetArray("c", c);
 
process.stdout.write("c: ");
for (let byte of c) {
    process.stdout.write(byte + " ");
}
 
 
 
/*
run:
 
a: 01000001 01100101 01110010 01111001 01101110 
b: 01000001 01101100 01100010 01110101 01110011 
c: 00000000 00001001 00010000 00001100 00011101 
c: 0 9 16 12 29 
 
*/

 



answered Jul 12, 2025 by avibootz
edited Jul 12, 2025 by avibootz

Related questions

1 answer 113 views
113 views asked Jul 12, 2025 by avibootz
1 answer 126 views
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
...