views:

475

answers:

3

I know this question has probably been asked in this forum many times and in the web as well. I am asked to create an implementation of a big integer in c++, however there is a constraint that one of my constructor should take an int as an argument... so I am guessing there will be more than one non-default constructor... so my question is, what would be the easiest way to do this??

A: 

Why reinvent the wheel? Use the GNU MP library.

[EDIT] Smells like homework. So when you have a BigBit class, then do this:

  1. Clear all bits
  2. Write a loop which goes over all bits on the int argument of the constructor
  3. For each bit in the int argument which is != 0, set the bit in the BigBit vector.
Aaron Digulla
Alex
that is also if the argument is an int, we're talking about large numbers here.. so an int wouldn't hold it... what if the argument is a string, how would you convert the string to bits.... representing that large number
Alex
Look at the source code for `atoi()` how to convert a string to its binary representation. It's pretty simple. See koders.com for an example: http://www.koders.com/c/fid85C526B4C012C3A19A83B32509327C77C9AC5598.aspx?s=atoi.c#L1
Aaron Digulla
+1  A: 

C++ BigInt class
C++ Big Integer Library
to write big int for example :

typedef struct {
    int high, low;
} BiggerInt;

BiggerInt add( const BiggerInt *lhs, const BiggerInt *rhs ) {
    BiggerInt ret;

    /* Ideally, you'd want a better way to check for overflow conditions */
    if ( rhs->high < INT_MAX - lhs->high ) {
        /* With a variable-length (a real) BigInt, you'd allocate some more room here */
    }

    ret.high = lhs->high + rhs->high;

    if ( rhs->low < INT_MAX - lhs->low ) {
        /* No overflow */
        ret.low = lhs->low + rhs->low;
    }
    else {
        /* Overflow */
        ret.high += 1;
        ret.low = lhs->low - ( INT_MAX - rhs->low ); /* Right? */
    }

    return ret;
}
SjB
+1  A: 

The question, then, seems to be "how do I turn an integer into a list of bits"? Put another way, what's the base-2 representation of an integer?

As this is supposed to be homework, let me talk around the problem by thinking in base-10; the appropriate changes should be obvious with some thought.

Given a base 10 number, it's pretty easy to figure out what the rightmost digit is: It's just the remainder when dividing by 10. E.g. if n=1234, then it's rightmost digit is n%10 = 4. To get the next rightmost digit, we divide by 10 (getting 123), and repeat the process. So:

1234/10=123; 1234%10 = 4
123/10=12  ; 123%10 = 3
12/10=1    ; 12%10 = 2
1/10=0     ; 1%10 = 1

So now we've gotten the answers [4,3,2,1]. If we reverse them, we have the base-10 digits of our number: [1, 2, 3, 4].

Managu
this works if the integer is small... what if the integer is large such as 2^10... definitely I need to store this in a variable first before converting it to bits... now the problem is what kind of variable would be able to store it... a string would do it, but then how do I convert a string to a binary ....
Alex
2^10 isn't really that large. And the algorithm I hint at takes only one division and one modulo per digit of the answer. It'll work fine if the number you start with fits in a machine word (e.g. unsigned int). And if it doesn't, what are we trying to convert again?
Managu