Skip to content

BigInteger losing precision (byte arrays) #42840

@agowa

Description

@agowa

Description

Biginteger loses precision when working with byte arrays.
([bigint](([System.Net.IPAddress]"::ffff:169.254.1.1").GetAddressBytes()) -band [bigint](([System.Net.IPAddress]"::ffff:169.254.0.0").GetAddressBytes())).ToByteArray() -join " "
Results in:
0 0 0 0 0 0 0 0 0 0 255 255 169 254 0
instead of
0 0 0 0 0 0 0 0 0 0 255 255 169 254 0 0

because of:
([IPAddress]'::ffff:169.254.0.0').GetAddressBytes() -join " " returns 0 0 0 0 0 0 0 0 0 0 255 255 169 254 0 0
and ([BigInt]([IPAddress]'::ffff:169.254.0.0').GetAddressBytes()).ToByteArray() -join " " loses that precision and only returns 0 0 0 0 0 0 0 0 0 0 255 255 169 254 0 instead.

alternatively to "fixing" this precision issue in biginteger, also implementing the binary operators for [System.Byte[]] would work, so that a byte array doesn't need to be converted to a biginteger at all.

Configuration

dotnet --list-runtimes
Microsoft.NETCore.App 3.1.8 [/usr/share/dotnet/shared/Microsoft.NETCore.App]
On x64 ArchLinux

Regression?

Other information

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.NumericsquestionAnswer questions and provide assistance, not an issue with source code or documentation.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions