Skip to content

Incorrect results of right shifting huge BigIntegers #65633

Closed

Description

@gaazkam

Description

Enormous negative BigIntegers produce -1 when right shifted by any value.

Reproduction Steps

using System.Numerics;

BigInteger bigInt = new BigInteger(-1) << int.MaxValue;
Console.WriteLine(bigInt.GetBitLength());
BigInteger shiftedBigInt = bigInt >> 1;
Console.WriteLine(shiftedBigInt.GetBitLength());
Console.WriteLine(shiftedBigInt == new BigInteger(-1));

Expected behavior

The program should output:

2147483647
2147483646
False

Actual behavior

The program outputs:

2147483647
0
True

Regression?

No response

Known Workarounds

No response

Configuration

Which version of .NET is the code running on?

<TargetFramework>net6.0</TargetFramework>

What OS and version, and what distro if applicable?

Windows 11 Pro Version 21H2

What is the architecture (x64, x86, ARM, ARM64)?

x64

Other information

  • Positive BigIntegers seem to not suffer from this issue, even if they are as enormous: (new BigInteger(1) << int.MaxValue) >> 1 seems to give expected results.
  • Slightly less enormous negative BigIntegers also seem to not suffer from this issue: (new BigInteger(-1) << (int.MaxValue - 1000)) >> 1 seems to give expected results. (new BigInteger(-1) << (int.MaxValue - 10)) >> 1, however, still returns -1.
  • How much we right shift the enormous big integer does not seem to affect the result. (new BigInteger(-1) << int.MaxValue) >> (int.MaxValue - 4) still gives -1.

Activity

  1. ghost added
    untriaged
    on Feb 20, 2022
  2. added and removed
    untriaged
    on Mar 14, 2022
  3. added this to the 7.0.0 milestone on Mar 14, 2022
  4. jeffhandley commented on Mar 30, 2022

    @jeffhandley
    Member

    @tannergooding Do you think this bug can be fixed without the significant refactoring we've been talking about for BigInteger, or should we wait to address it until we have that work scheduled?

  5. tannergooding commented on Mar 31, 2022

    @tannergooding
    Member

    Yes, it should be. It would probably be easier after, but I don't think that its necessary or good to wait if someone has time to look at this for .NET 7

  6. added 2 commits that reference this issue on Apr 11, 2022
    8f684fc
    c13de7b
  7. ghost added
    in-pr
    on Apr 11, 2022
  8. jeffhandley commented on Apr 11, 2022

    @jeffhandley
    Member

    Per discussion with the team, we will consider backporting the fix for this issue into .NET 6 if we can get validation that the user's scenario is addressed with the fix. We'd also need to get information that describes the types of applications affected, if the issues are blocking production scenarios, and an understanding of when .NET 7 would be adopted into that environment.

  9. ghost removed
    in-pr
    on Apr 19, 2022
  10. gaazkam commented on May 10, 2022

    @gaazkam
    Author

    Per discussion with the team, we will consider backporting the fix for this issue into .NET 6 if we can get validation that the user's scenario is addressed with the fix. We'd also need to get information that describes the types of applications affected, if the issues are blocking production scenarios, and an understanding of when .NET 7 would be adopted into that environment.

    I am sorry for my late reply.

    But no, this doesn't block any production scenarios. I hit this issue when I was trying to self-educate by implementing a program that would compute lots of digits of pi. This has never been intended to be used in production and even for this purpose I don't need this functionality.

  11. ghost locked as resolved and limited conversation to collaborators on Jun 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions