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

56,142 answers

573 users

How to XOR byte arrays in C#

1 Answer

0 votes
using System;

class XORBytesDemo
{
    // XOR two byte arrays of the same length
    static byte[] XorBytes(byte[] a, byte[] b) {
        byte[] result = new byte[a.Length];
        for (int i = 0; i < a.Length; i++) {
            result[i] = (byte)(a[i] ^ b[i]);
        }
        return result;
    }

    // Print byte array as 8-bit binary strings
    static void PrintBitsetArray(string label, byte[] array) {
        Console.Write(label + ": ");
        foreach (byte b in array) {
            Console.Write(Convert.ToString(b, 2).PadLeft(8, '0') + " ");
        }
        Console.WriteLine();
    }

    static void Main()
    {
        byte[] a = { (byte)'A', (byte)'e', (byte)'r', (byte)'y', (byte)'n' };
        byte[] b = { (byte)'A', (byte)'l', (byte)'b', (byte)'u', (byte)'s' };

        byte[] c = XorBytes(a, b);

        PrintBitsetArray("a", a);
        PrintBitsetArray("b", b);
        PrintBitsetArray("c", c);

        Console.Write("c: ");
        foreach (byte ch in c) {
            Console.Write((int)ch + " ");
        }
    }
}


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

Related questions

1 answer 149 views
149 views asked Jul 12, 2025 by avibootz
1 answer 151 views
1 answer 136 views
136 views asked Jul 12, 2025 by avibootz
1 answer 141 views
141 views asked Jul 12, 2025 by avibootz
1 answer 161 views
161 views asked Jul 12, 2025 by avibootz
1 answer 171 views
1 answer 145 views
...