1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Custom Data Type for Huge Numbers

Discussion in 'C++' started by rivaskh, Aug 9, 2008.

  1. rivaskh

    rivaskh New Member


    Is there any way that I can do a following initialization?

    DataType abc = 22222222222222222222222;

    where the value is a huge number larger than 2^32.
  2. erislover

    erislover New Member

    Probably not. But check out gmplib.org for a library that could help you.
  3. erislover

    erislover New Member

    Well, by "probably not" I'm assuming you need something larger than 64 bits, available on every 32-bit system I'm aware of. If you're working in embedded systems where 32 bits is already your largest built-in data type, then you must code up your own functions and declarations to deal with larger numbers. This is much easier in C++ than C because of operator overloading. If you are working with C++ then google will help you find plenty of good resources for hand-rolling what you need, if you don't want to use GMP.
  4. xpi0t0s

    xpi0t0s Mentor

    No, because 22222222222222222222222 is an invalid number and will be picked up by the compiler as an error.
    However you could encode the number differently, for example using strings:
    DataType abc = "22222222222222222222222";
  5. rivaskh

    rivaskh New Member

    True... I was wondering if I could create a a class that coupled a lot of integers together to form a super integer that could take values higher than 32 bit with it having the same operations as a primitive integer type. This way, I could use it in place of unsigned int and still retain my code without too much change.
  6. xpi0t0s

    xpi0t0s Mentor

    Yes of course, that's the whole point of operator overloading in C++. In C++ you can fairly easily write something like
    MyBigNumCls num1(23,75,62); // a constructor that takes 3 integers
    MyBigNumCls num2(49,11,51);
    MyBigNumCls result=num1+num2;
    So num1(23,75,62) could represent 23<<64 | 75<<32 | 62 and it's up to you how to store it, as three integers if you like. With the above code result, assuming you've defined MyBigNumCls::eek:perator +(), would then represent (23+49)<<64 | (75+11)<<32 | (62+51)

    This is easy to do; have a go and see how far you get. For multiplication and division just look back to how you used to do it at school on paper then implement that algorithm.

Share This Page