Skip to content

IBinaryInteger<BigInteger>.GetShortestBitLength() - int overflow is not handled #84670

@speshuric

Description

@speshuric

Description

When bit length of BigInteger value is greater than 2^(int.MaxValue+1) - 1, then IBinaryInteger<BigInteger>.GetShortestBitLength() can not return correct result because bit length is more than int.MaxValue.
Now IBinaryInteger<BigInteger>.GetShortestBitLength() returns incorrect negative value.

Reproduction Steps

Code:

using System.Numerics;
var l1 = 42;
var l2 = int.MaxValue;
BigInteger b = (BigInteger.One<<l1)<<l2;
var ll = 1L + l1 + l2;
Console.WriteLine(ll);
var res = ((IBinaryInteger<BigInteger>)b).GetShortestBitLength();
Console.WriteLine(res);

Expected behavior

Exception on GetShortestBitLength call. Result can not be presented in int type.

Actual behavior

output

2147483690
-2147483606

Regression?

No

Known Workarounds

No

Configuration

The latest main branch

Other information

probably from PR #69391
cc @tannergooding

It seems fix is quite trivial, I can do it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions