2015-05-25 5 views
0

есть способ Ror всего byte[] определенной суммы? Я уже сделал некоторые исследования и нашел решение Рол byte[]:Ror byte array with C#

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; 
} 

Автор этого кода можно найти здесь: Is there a function to do circular bitshift for a byte array in C#?

Любая идея, как я могу сделать то же самое, но выполнить Операция Ror вместо операции Rol на byte[]?

+3

ROR из 'x' битов эквивалентно ROL из' arr.length * 8 - x' – xanatos

+0

Возможные Дубликат: http://stackoverflow.com/questions/15561607/is-there-a- функция-To-Do-круговой Bitshift-для-а-байт-массив в-с? LQ = 1 –

ответ

1
static byte[] ROR_ByteArray(byte[] arr, int nShift) 
{ 
    return ROL_ByteArray(arr, arr.Length*8-nShift); 
}