shift int array Right - CSharp System

CSharp examples for System:Array Shift

Description

shift int array Right

Demo Code

// All rights reserved.
using System;/*from ww  w.  j a  va2 s  .  co m*/

public class Main{
        private static int shiftRight(uint[] buffer, int shiftVal)
    {
        int shiftAmount = 32;
        int invShift = 0;
        int bufLen = buffer.Length;

        while (bufLen > 1 && buffer[bufLen - 1] == 0)
            bufLen--;

        //Console.WriteLine("bufLen = " + bufLen + " buffer.Length = " + buffer.Length);

        for (int count = shiftVal; count > 0; )
        {
            if (count < shiftAmount)
            {
                shiftAmount = count;
                invShift = 32 - shiftAmount;
            }

            //Console.WriteLine("shiftAmount = {0}", shiftAmount);

            ulong carry = 0;
            for (int i = bufLen - 1; i >= 0; i--)
            {
                ulong val = ((ulong)buffer[i]) >> shiftAmount;
                val |= carry;

                carry = ((ulong)buffer[i]) << invShift;
                buffer[i] = (uint)(val);
            }

            count -= shiftAmount;
        }

        while (bufLen > 1 && buffer[bufLen - 1] == 0)
            bufLen--;

        return bufLen;
    }
}

Related Tutorials