0

is there a way to Ror an entire byte[] by a specific amount? I've already done some research and found a solution to Rol a byte[] :

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

The author of this code can be found here: Is there a function to do circular bitshift for a byte array in C#?

Any idea how I can do the same thing but perform a Ror operation instead of a Rol operation on a byte[] ?

2
  • 3
    a ror of x bits is equivalent to a rol of arr.Length * 8 - x Commented May 25, 2015 at 17:34
  • Possible duplicate: stackoverflow.com/questions/15561607/… Commented May 25, 2015 at 17:57

1 Answer 1

1
static byte[] ROR_ByteArray(byte[] arr, int nShift)
{
  return ROL_ByteArray(arr, arr.Length*8-nShift);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.