none
high integers RRS feed

  • Question

  • I was wondering what if I need a number larger than any of the .NET data types how can I define a new data type? (if their is a large difference In thee syntax then plz provide the C#)

    Thanks,

    Nosyarg23

    Saturday, September 22, 2012 11:55 AM

Answers

  • If yo uneed a new integer type define a class for the large number and handle all operations within the class.  I suspect you would want to store large number as bytes and create functions to add, subtract, multiple, and divide.

           public class highnumber
            {
                const int NSIZE = 128;
                byte[] number = new byte[NSIZE];
                public highnumber() { }
                byte[] bytes = new byte[NSIZE];
                public byte[] Add(long number1, long number2)
                {
                    int carry = 0;
                    int place = 0;
                    for (int i = 0; i < 8; i++)
                    {
                        place = (int)(number1 & 0xff) + (int)(number2 & 0xff) + carry;
                        if (place > 255)
                        {
                            number[i] = (byte)(place & 0xff);
                            carry = place >> 8;
                        }
                        else
                        {
                            number[i] = (byte)place;
                            carry = 0;
                        }
                    }
                    return number;
                }
            }


    jdweng

    Saturday, September 22, 2012 1:05 PM

All replies

  • If yo uneed a new integer type define a class for the large number and handle all operations within the class.  I suspect you would want to store large number as bytes and create functions to add, subtract, multiple, and divide.

           public class highnumber
            {
                const int NSIZE = 128;
                byte[] number = new byte[NSIZE];
                public highnumber() { }
                byte[] bytes = new byte[NSIZE];
                public byte[] Add(long number1, long number2)
                {
                    int carry = 0;
                    int place = 0;
                    for (int i = 0; i < 8; i++)
                    {
                        place = (int)(number1 & 0xff) + (int)(number2 & 0xff) + carry;
                        if (place > 255)
                        {
                            number[i] = (byte)(place & 0xff);
                            carry = place >> 8;
                        }
                        else
                        {
                            number[i] = (byte)place;
                            carry = 0;
                        }
                    }
                    return number;
                }
            }


    jdweng

    Saturday, September 22, 2012 1:05 PM
  • You can't need a number larger than what can be represented by the BigInteger class.
    Saturday, September 22, 2012 1:11 PM
  • well for mathematical functions which I want to do with it I need the highest number I can get
    Saturday, September 22, 2012 7:15 PM
  • well for mathematical functions which I want to do with it I need the highest number I can get
    Declare a BigInteger containing the highest number you can think of and add 1 to it.  If that isn't high enough, multiply the highest number by the highest number.
    Saturday, September 22, 2012 7:35 PM
  • I also think that BigInteger will be good enough.
    Saturday, September 22, 2012 8:07 PM
  • Just use this constructor:

    public BigInteger(
    	byte[] value
    )

    The .NET limit on the object size in memory is the only limit on how big the number can be. I heard that .NET 4.5 with Visual Studio 2012 now allows arrays larger than 2 GB on a 64-bit system. On a 32-bit system, you are limited to 2 GB of memory for your entire application.


    Dan Randolph - My Code Samples List

    Sunday, September 23, 2012 3:16 PM