简体   繁体   English

c# 在字节数组上应用 64 位 XOR

[英]c# Apply 64 bit XOR on byte Array

I want to Apply 64 XOR operation of two byte array.我想应用两个字节数组的 64 XOR 操作。 Is this right approach to do with using unsafe这是使用unsafe的正确方法吗

I have tried below approach without using unsafe .我尝试了以下方法而不使用unsafe but i want little faster than this但我想要比这快一点

for (int i=0; i< oldBlock.Length;i++)
{
{
 oldblock[i] ^= (newblock[i]);
}

Below XOR operation miss last bytes as below code XOR 8 bytes each time.下面的 XOR 操作错过了最后一个字节,如下代码 XOR 每次 8 个字节。

How to accomplish this.如何做到这一点。

static void Main(string[] args)
        {


            byte[] a = new byte[10];
            byte[] b = new byte[10];
            Random r = new Random();
            r.NextBytes(a);
            a.CopyTo(b, 0);
            XOr64(a, b);
            foreach (byte c in a)
            {
                Console.WriteLine(c);
            }


            Console.ReadKey();



        }    

public static unsafe void XOr64(byte[] oldBlock, byte[] newblock)
                {
                    try
                    {
                        fixed (byte* byteA = oldBlock)
                        fixed (byte* byteB = newblock)
                        {
                            long* ppA = (long*)byteA;
                            long* ppB = (long*)byteB;

                            for (int p = 0; p < oldBlock.Length/8; p++)
                            {
                                *ppA ^= *ppB;

                                ppA++;
                                ppB++;
                            }
                        }
                    }
                    catch
                    {

                    }



                }

If the 8-byte-at-a-time aspect is working well for you and you're sure you need the extra performance, you can just extend that method to cover the remaining bytes individually - which will be at most 7 bytes:如果一次 8 个字节的特性对您来说效果很好,并且您确定需要额外的性能,您可以扩展该方法以单独覆盖剩余的字节 - 最多 7 个字节:

public static unsafe void XOr64(byte[] oldBlock, byte[] newBlock)
{
    // First XOR as many 64-bit blocks as possible, for the sake of speed
    fixed (byte* byteA = oldBlock)
    fixed (byte* byteB = newBlock)
    {
        long* ppA = (long*) byteA;
        long* ppB = (long*) byteB;

        int chunks = oldBlock.Length / 8;
        for (int p = 0; p < chunks; p++)
        {
            *ppA ^= *ppB;

            ppA++;
            ppB++;
        }
    }

    // Now cover any remaining bytes one byte at a time. We've
    // already handled chunks * 8 bytes, so start there.
    for (int index = chunks * 8; index < oldBlock.Length; index++)
    {
        oldBlock[index] ^= newBlock[index];
    }
}

Here is @Jon Skeet algorithm implemented using Span<> instead of unsafe code:这是使用Span<>而不是不安全代码实现的@Jon Skeet 算法:

public static void Xor64(Span<byte> bytes, ReadOnlySpan<byte> mask) {
    int chunks = mask.Length / 8;
    int chunksBounds = chunks * 8;
    Xor64(MemoryMarshal.Cast<byte, long>(bytes[..chunksBounds]), MemoryMarshal.Cast<byte, long>(mask[..chunksBounds]));
    for (int i = chunksBounds;i < mask.Length;i++) {
        bytes[i] ^= mask[i];
    }
}
public static void Xor64(Span<long> longs, ReadOnlySpan<long> mask) {
    for (int i = 0;i < longs.Length;i++) {
        longs[i] ^= mask[i];
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM