Skip to content

Commit

Permalink
Fix BigInteger bitwise operators on certain negative numbers (#109684)
Browse files Browse the repository at this point in the history
Co-authored-by: Tanner Gooding <tagoo@outlook.com>
  • Loading branch information
Rob-Hague and tannergooding authored Nov 13, 2024
1 parent 4095634 commit d3d0fce
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ private BigInteger(Span<uint> value)

if ((length == 0) || ((int)value[length - 1] >= 0))
{
// We ne need to preserve the sign bit
// We need to preserve the sign bit
length++;
}
Debug.Assert((int)value[length - 1] < 0);
Expand Down
16 changes: 16 additions & 0 deletions src/libraries/System.Runtime.Numerics/tests/BigInteger/op_and.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,22 @@ public static void RunAndTests()
}
}

[Fact]
public void Issue109669()
{
// Operations on numbers whose result is of the form 0xFFFFFFFF 00000000 ... 00000000
// in two's complement.

Assert.Equal(-4294967296, new BigInteger(-4294967296) & new BigInteger(-1919810));
Assert.Equal(-4294967296, new BigInteger(-4042322161) & new BigInteger(-252645136));
Assert.Equal(-4294967296, new BigInteger(-8589934592) | new BigInteger(-21474836480));

BigInteger a = new BigInteger(MemoryMarshal.AsBytes([uint.MaxValue, 0u, 0u]), isBigEndian: true);
Assert.Equal(a, a & a);
Assert.Equal(a, a | a);
Assert.Equal(a, a ^ 0);
}

[Fact]
public static void RunAndTestsForSampleSet1()
{
Expand Down

0 comments on commit d3d0fce

Please sign in to comment.