如何在BigInteger的存储值在内部? [英] How does the BigInteger store values internally?

查看:136
本文介绍了如何在BigInteger的存储值在内部?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

BigInteger类有返回一个字节数组的方法。是表示该类使用的字节数组内部以及存储数字的事实?

The BigInteger class has a method that returns a byte array. Is that indicative of the fact that the class uses a byte array internally as well to store numbers?

这知道的是重要的,以便选择正确的数据类型来处理二进制数据。如果类是,例如,使用一个Int64数组,那么类似的阵列将通过任何调用函数是更有效地操纵的原始数据

This is important to know in order to pick the right data types to manipulate binary data. If the class were, for example, to use an Int64 array, then a similar array would be more efficient to manipulate raw data by any calling functions.

作为一个例子, 。我打电话的ToByteArray方法来遍历查找特定的二进制模式字节

As an example, I am calling the ToByteArray method to traverse bytes looking for specific binary patterns.

推荐答案

据的 它的参考源看来,它存储作为一个 UINT [信息] 的数据和符号的int值。

According to the reference source it appears that it stores it's information as a uint[] for the data and a int for the sign.

namespace System.Numerics
{
    /// <summary>Represents an arbitrarily large signed integer.</summary>
    [Serializable]
    public struct BigInteger : IFormattable, IComparable, IComparable<BigInteger>, IEquatable<BigInteger>
    {

    // For values int.MinValue < n <= int.MaxValue, the value is stored in sign
    // and _bits is null. For all other values, sign is +1 or -1 and the bits are in _bits
    internal int _sign;
    internal uint[] _bits;

下面是执行以执行代码 ToByteArray()

Here is the code it executes to perform ToByteArray()

    // Return the value of this BigInteger as a little-endian twos-complement
    // byte array, using the fewest number of bytes possible. If the value is zero,
    // return an array of one byte whose element is 0x00.
    public byte[] ToByteArray() {
        if (_bits == null && _sign == 0)
            return new byte[] { 0 };

        // We could probably make this more efficient by eliminating one of the passes.
        // The current code does one pass for uint array -> byte array conversion,
        // and then another pass to remove unneeded bytes at the top of the array.
        uint[] dwords;
        byte highByte;

        if (_bits == null) {
            dwords = new uint[] { (uint)_sign };
            highByte = (byte)((_sign < 0) ? 0xff : 0x00);
        }
        else if(_sign == -1) {
            dwords = (uint[])_bits.Clone();
            NumericsHelpers.DangerousMakeTwosComplement(dwords);  // mutates dwords
            highByte = 0xff;
        } else {
            dwords = _bits;
            highByte = 0x00;
        }

        byte[] bytes = new byte[checked(4 * dwords.Length)];
        int curByte = 0;
        uint dword;
        for (int i = 0; i < dwords.Length; i++) {
            dword = dwords[i];
            for (int j = 0; j < 4; j++) {
                bytes[curByte++] = (byte)(dword & 0xff);
                dword >>= 8;
            }
        }

        // find highest significant byte
        int msb;
        for (msb = bytes.Length - 1; msb > 0; msb--) {
            if (bytes[msb] != highByte) break;
        }
        // ensure high bit is 0 if positive, 1 if negative
        bool needExtraByte = (bytes[msb] & 0x80) != (highByte & 0x80);

        byte[] trimmedBytes = new byte[msb + 1 + (needExtraByte ? 1 : 0)];
        Array.Copy(bytes, trimmedBytes, msb + 1);

        if (needExtraByte) trimmedBytes[trimmedBytes.Length - 1] = highByte;
        return trimmedBytes;
    }

这篇关于如何在BigInteger的存储值在内部?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆