Fix Shift Operator Bugs

Fixed array order bug in Int128 and Int256 shift operators (<< and >>)

Added a ShiftRightSigned to MathUtils

Used  ShiftRightSigned for Int128 and Int256 right shift operator (>>) to preserve sign when right shifting negative numbers

Added test cases for Int128 and Int256 shift operators


The existing shiftright doesn't work with negative numbers. int128 and int256 can be negative and lost their sign with the old shiftright.

Other code expects the shiftright to work with unsigned arrays, so I didn't want to change its behavior. There's a unit test for the mathutils for instance that needs the shiftright to be unsigned.

Sent from my iPhone

On Oct 11, 2014, at 3:02 PM, "Alexander Logger" <notifications@github.com<mailto:notifications@github.com>> wrote:


In src/BigMath/BigMath.PCL/Utils/MathUtils.cs:

> @@ -71,6 +72,61 @@ public static ulong[] ShiftRight(ulong[] values, int shift)
>
>          /// <summary>
>          ///     Bitwise right shift.
> +        ///
> +        ///     Using an array of ulong's, but when called from Int128 and Int256, value is really a signed number, so we need to preserve the sign bits
> +        /// </summary>
> +        /// <param name="values">Bits to shift. Lower bits have lower index in array.</param>
> +        /// <param name="shift">Shift amount in bits.</param>
> +        /// <returns>Shifted values.</returns>
> +        public static ulong[] ShiftRightSigned(ulong[] values, int shift)


Why don't you just edit existed method ShiftRight()?

—
Reply to this email directly or view it on GitHub<https://github.com/Taggersoft/BigMath/pull/6/files#r18744307>.