Search code examples
arraysvb.netbytexor

Most efficient way to xor byte array in vb.net


I am making making an application in vb.net that will read files using a byte array as buffer, and create parity files for them by xoring the data... What would be the most efficient way of xoring a byte array? I have though of convertinbg the byte array to a bitarray and then run it trough an xor operation and turn it back to a byte array, but that sounds like a very processing expensive task, and i am worried that it might impact read/write speed... is there a better way to do this? thanks...

To avoid confusion: What the application does is read half the file to location 1, the other half to location 2, then a parity (xor of the two parts) to location 3...


Solution

  • To Xor two byte arrays simply use a for loop and the Xor operator.

    VB.Net's Xor will compile to a the CIL Xor opcode which should be subsequently JIT compiled to the very fast x86 XOR processor instruction.

    The cost of the Xor operation is likely to be negligible in comparison to the cost of file I/O.