Skip to content

RotateLeft and RotateRight of BigInteger is buggy. #112854

@kzrnm

Description

Description

See Reproduction Steps.

Reproduction Steps

Rotate((BigInteger.One << 64) - 1);
Rotate(BigInteger.One << 64);
Rotate((BigInteger.One << 64) + 1);
Rotate((BigInteger.One << 64) + 6);


Rotate((BigInteger.MinusOne << 64) - 1);
Rotate(BigInteger.MinusOne << 64);
Rotate((BigInteger.MinusOne << 64) + 1);
Rotate((BigInteger.MinusOne << 64) + 6);

void Rotate(BigInteger num)
{
    Console.Write("       Orig: "); WriteLine(num);
    Console.Write("RotateRight: "); WriteLine(BigInteger.RotateRight(num, 1));
    Console.Write(" RotateLeft: "); WriteLine(BigInteger.RotateLeft(num, 1));
    Console.WriteLine();
}
void WriteLine<T>(T num)
{
    Console.WriteLine($"{num:B96}");
}

Expected behavior

       Orig: 000000000000000000000000000000001111111111111111111111111111111111111111111111111111111111111111
RotateRight: 000000000000000000000000000000001111111111111111111111111111111111111111111111111111111111111111
 RotateLeft: 000000000000000000000000000000001111111111111111111111111111111111111111111111111111111111111111

       Orig: 000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000
RotateRight: 000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000
 RotateLeft: 000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000

       Orig: 000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001
RotateRight: 100000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000
 RotateLeft: 000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000010

       Orig: 000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000110
RotateRight: 000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000011
 RotateLeft: 000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000001100

       Orig: 111111111111111111111111111111101111111111111111111111111111111111111111111111111111111111111111
RotateRight: 000000000000000000000000000000010111111111111111111111111111111111111111111111111111111111111111
 RotateLeft: 111111111111111111111111111111011111111111111111111111111111111111111111111111111111111111111111

       Orig: 111111111111111111111111111111110000000000000000000000000000000000000000000000000000000000000000
RotateRight: 011111111111111111111111111111111000000000000000000000000000000000000000000000000000000000000000
 RotateLeft: 111111111111111111111111111111100000000000000000000000000000000000000000000000000000000000000001

       Orig: 111111111111111111111111111111110000000000000000000000000000000000000000000000000000000000000001
RotateRight: 111111111111111111111111111111111000000000000000000000000000000000000000000000000000000000000000
 RotateLeft: 111111111111111111111111111111100000000000000000000000000000000000000000000000000000000000000011

       Orig: 111111111111111111111111111111110000000000000000000000000000000000000000000000000000000000000110
RotateRight: 011111111111111111111111111111111000000000000000000000000000000000000000000000000000000000000011
 RotateLeft: 111111111111111111111111111111100000000000000000000000000000000000000000000000000000000000001101

Actual behavior

       Orig: 000000000000000000000000000000001111111111111111111111111111111111111111111111111111111111111111
RotateRight: 000000000000000000000000000000001111111111111111111111111111111111111111111111111111111111111111
 RotateLeft: 000000000000000000000000000000001111111111111111111111111111111111111111111111111111111111111111

       Orig: 000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000
RotateRight: 000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000
 RotateLeft: 000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000

       Orig: 000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001
RotateRight: 000000000000000000000000000000001000000000000000000000000000000010000000000000000000000000000000
 RotateLeft: 000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000010

       Orig: 000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000110
RotateRight: 000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000011
 RotateLeft: 000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000001100

       Orig: 111111111111111111111111111111101111111111111111111111111111111111111111111111111111111111111111
RotateRight: 111111111111111111111111111111111111111111111111111111111111111101111111111111111111111111111111
 RotateLeft: 111111111111111111111111111111011111111111111111111111111111111111111111111111111111111111111111

       Orig: 111111111111111111111111111111110000000000000000000000000000000000000000000000000000000000000000
RotateRight: 011111111111111111111111111111110000000000000000000000000000000010000000000000000000000000000000
 RotateLeft: 111111111111111111111111111111100000000000000000000000000000000000000000000000000000000000000001

       Orig: 111111111111111111111111111111110000000000000000000000000000000000000000000000000000000000000001
RotateRight: 111111111111111111111111111111111000000000000000000000000000000000000000000000000000000000000000
 RotateLeft: 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010

       Orig: 111111111111111111111111111111110000000000000000000000000000000000000000000000000000000000000110
RotateRight: 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000011
 RotateLeft: 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001100

Regression?

.NET 7 or later

Known Workarounds

No response

Configuration

No response

Other information

similar to #112564

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    area-System.Numericsbughelp wanted[up-for-grabs] Good issue for external contributorsin-prThere is an active PR which will close this issue when it is merged

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions