Skip to content

Commit 7245cf3

Browse files
committed
Add BigInteger.Rotate* tests
1 parent 2a51ee3 commit 7245cf3

File tree

4 files changed

+738
-1
lines changed

4 files changed

+738
-1
lines changed

src/libraries/System.Runtime.Numerics/tests/BigInteger/MyBigInt.cs

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ public static BigInteger DoBinaryOperatorMine(BigInteger num1, BigInteger num2,
110110
return new BigInteger(ShiftLeft(bytes1, Negate(bytes2)).ToArray());
111111
case "b<<":
112112
return new BigInteger(ShiftLeft(bytes1, bytes2).ToArray());
113+
case "bRotateLeft":
114+
return new BigInteger(RotateLeft(bytes1, bytes2).ToArray());
115+
case "bRotateRight":
116+
return new BigInteger(RotateLeft(bytes1, Negate(bytes2)).ToArray());
113117
case "b^":
114118
return new BigInteger(Xor(bytes1, bytes2).ToArray());
115119
case "b|":
@@ -774,6 +778,105 @@ public static List<byte> ShiftRight(List<byte> bytes)
774778
return bresult;
775779
}
776780

781+
public static List<byte> RotateRight(List<byte> bytes)
782+
{
783+
List<byte> bresult = new List<byte>();
784+
785+
byte bottom = (byte)(bytes[0] & 0x01);
786+
787+
for (int i = 0; i < bytes.Count; i++)
788+
{
789+
byte newbyte = bytes[i];
790+
791+
newbyte = (byte)(newbyte / 2);
792+
if ((i != (bytes.Count - 1)) && ((bytes[i + 1] & 0x01) == 1))
793+
{
794+
newbyte += 128;
795+
}
796+
if ((i == (bytes.Count - 1)) && (bottom != 0))
797+
{
798+
newbyte += 128;
799+
}
800+
bresult.Add(newbyte);
801+
}
802+
803+
return bresult;
804+
}
805+
806+
public static List<byte> RotateLeft(List<byte> bytes)
807+
{
808+
List<byte> bresult = new List<byte>();
809+
810+
bool prevHead = (bytes[bytes.Count - 1] & 0x80) != 0;
811+
812+
for (int i = 0; i < bytes.Count; i++)
813+
{
814+
byte newbyte = bytes[i];
815+
816+
newbyte = (byte)(newbyte * 2);
817+
if (prevHead)
818+
{
819+
newbyte += 1;
820+
}
821+
822+
bresult.Add(newbyte);
823+
824+
prevHead = (bytes[i] & 0x80) != 0;
825+
}
826+
827+
return bresult;
828+
}
829+
830+
831+
public static List<byte> RotateLeft(List<byte> bytes1, List<byte> bytes2)
832+
{
833+
List<byte> bytes1Copy = Copy(bytes1);
834+
int byteShift = (int)new BigInteger(Divide(Copy(bytes2), new List<byte>(new byte[] { 8 })).ToArray());
835+
sbyte bitShift = (sbyte)new BigInteger(Remainder(bytes2, new List<byte>(new byte[] { 8 })).ToArray());
836+
837+
Trim(bytes1);
838+
839+
byte fill = (bytes1[bytes1.Count - 1] & 0x80) != 0 ? byte.MaxValue : (byte)0;
840+
841+
if (fill == 0 && bytes1.Count > 1 && bytes1[bytes1.Count - 1] == 0)
842+
bytes1.RemoveAt(bytes1.Count - 1);
843+
844+
while (bytes1.Count % 4 != 0)
845+
{
846+
bytes1.Add(fill);
847+
}
848+
849+
byteShift %= bytes1.Count;
850+
if (byteShift == 0 && bitShift == 0)
851+
return bytes1Copy;
852+
853+
for (int i = 0; i < Math.Abs(bitShift); i++)
854+
{
855+
if (bitShift < 0)
856+
{
857+
bytes1 = RotateRight(bytes1);
858+
}
859+
else
860+
{
861+
bytes1 = RotateLeft(bytes1);
862+
}
863+
}
864+
865+
List<byte> temp = new List<byte>();
866+
for (int i = 0; i < bytes1.Count; i++)
867+
{
868+
temp.Add(bytes1[(i - byteShift + bytes1.Count) % bytes1.Count]);
869+
}
870+
bytes1 = temp;
871+
872+
if (fill == 0)
873+
bytes1.Add(0);
874+
875+
Trim(bytes1);
876+
877+
return bytes1;
878+
}
879+
777880
public static List<byte> SetLength(List<byte> bytes, int size)
778881
{
779882
List<byte> bresult = new List<byte>();

0 commit comments

Comments
 (0)