是否有一个函数在C#中的字节数组做圆周bitshift? [英] Is there a function to do circular bitshift for a byte array in C#?

查看:151
本文介绍了是否有一个函数在C#中的字节数组做圆周bitshift?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法找到,如果有一个内置的方式做一个字节数组的圆形bitshift,什么C的的 ROL和ROR 的使用单个字节办?

让我解释一下,说,我有一个数组(二进制):

  [0] = 11001110
[1] = 01000100
[2] = 10100001

,然后如果我想要做的 ROL_Array(1位)或移动比特1比特的左边,我会得到:

  [0] = 10011100
[1] = 10001001
[2] = 01000011

或者,如果我想要做的 ROR_Array(2位)或移动位2位的权利,我会得到:

  [0] = 00110011
[1] = 01010001
[2] = 10101000


解决方案

这并不像你想象的那么简单。这里有一个快速版本的线程被关闭前:

 公共静态的byte [] ROL_ByteArray(字节[]编曲,诠释nShift)
{
    //执行'改编'由'nShift位向左逐位循环移位
    //返回:
    // =结果
    字节[] = resArr新的字节[arr.Length]    如果(arr.Length大于0)
    {
        INT nByteShift = nShift /(的sizeof(字节)* 8); // @ dasblinkenlight的修正后调整
        INT nBitShift = nShift%(的sizeof(字节)* 8);        如果(nByteShift> = arr.Length)
            nByteShift%= arr.Length;        INT S = arr.Length - 1;
        INT D =秒 - nByteShift;        对于(INT nCnt = 0; nCnt< arr.Length; nCnt ++,D--,S--)
        {
            而(D℃,)
                D + = arr.Length;
            而(S℃,)
                S + = arr.Length;            字节字节= ARR [S]。            resArr [D]。| =(字节)(字节<< nBitShift);
            resArr [D> 0? ð - 1:resArr.Length - 1] | =(字节)(字节>>(的sizeof(字节)* 8 - nBitShift));
        }
    }    返回resArr;
}

和这里的测试:

 字节[] =改编新的字节[] {
    Convert.ToByte(11001110,2),
    Convert.ToByte(01000100,2),
    Convert.ToByte(10100001,2),
    };字节[] = ARR2 Auth.ROL_ByteArray(ARR,1);字符串SSS =;
的for(int i = 0; I< arr2.Length;我++)
    SSS + = Convert.ToString(ARR2 [I],2)+,;的Debug.WriteLine(SSS);

I can't seem to find if there's a built-in way to do a circular bitshift of a byte array, what C's ROL and ROR used to do with a single byte?

Let me explain, say, I have an array (in binary):

[0] = 11001110
[1] = 01000100
[2] = 10100001

and then if I want to do ROL_Array(1 bit) or move bits 1 bit to the left, I'd get:

[0] = 10011100
[1] = 10001001
[2] = 01000011

or, if I want to do ROR_Array(2 bits) or move bits 2 bits to the right, I'd get:

[0] = 00110011
[1] = 01010001
[2] = 10101000

解决方案

This is not as simple as you'd think. Here's a quick version before this thread gets closed:

public static byte[] ROL_ByteArray(byte[] arr, int nShift)
{
    //Performs bitwise circular shift of 'arr' by 'nShift' bits to the left
    //RETURN:
    //      = Result
    byte[] resArr = new byte[arr.Length];

    if(arr.Length > 0)
    {
        int nByteShift = nShift / (sizeof(byte) * 8);   //Adjusted after @dasblinkenlight's correction
        int nBitShift = nShift % (sizeof(byte) * 8);

        if (nByteShift >= arr.Length)
            nByteShift %= arr.Length;

        int s = arr.Length - 1;
        int d = s - nByteShift;

        for (int nCnt = 0; nCnt < arr.Length; nCnt++, d--, s--)
        {
            while (d < 0)
                d += arr.Length;
            while (s < 0)
                s += arr.Length;

            byte byteS = arr[s];

            resArr[d] |= (byte)(byteS << nBitShift);
            resArr[d > 0 ? d - 1 : resArr.Length - 1] |= (byte)(byteS >> (sizeof(byte) * 8 - nBitShift));


        }
    }

    return resArr;
}

and here's a test:

byte[] arr = new byte[] {
    Convert.ToByte("11001110", 2),
    Convert.ToByte("01000100", 2),
    Convert.ToByte("10100001", 2),
    };

byte[] arr2 = Auth.ROL_ByteArray(arr, 1);

string sss = "";
for (int i = 0; i < arr2.Length; i++)
    sss += Convert.ToString(arr2[i], 2) + ", ";

Debug.WriteLine(sss);

这篇关于是否有一个函数在C#中的字节数组做圆周bitshift?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆