shift int array Left - CSharp System

CSharp examples for System:Array Shift

Description

shift int array Left

Demo Code

// All rights reserved.
using System;/*w w  w. j  a  v  a  2s. com*/

public class Main{
        // least significant bits at lower part of buffer

    private static int shiftLeft(uint[] buffer, int shiftVal)
    {
        int shiftAmount = 32;
        int bufLen = buffer.Length;

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

        for (int count = shiftVal; count > 0; )
        {
            if (count < shiftAmount)
                shiftAmount = count;

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

            ulong carry = 0;
            for (int i = 0; i < bufLen; i++)
            {
                ulong val = ((ulong)buffer[i]) << shiftAmount;
                val |= carry;

                buffer[i] = (uint)(val & 0xFFFFFFFF);
                carry = val >> 32;
            }

            if (carry != 0)
            {
                if (bufLen + 1 <= buffer.Length)
                {
                    buffer[bufLen] = (uint)carry;
                    bufLen++;
                }
            }
            count -= shiftAmount;
        }
        return bufLen;
    }
}

Related Tutorials