views:

425

answers:

8

In C++, how do I combine (note: not add) two integers into one big integer?

For example:

int1 = 123;
int2 = 456;

Is there a function to take the two numbers and turn intCombined into 123456?

EDIT:

My bad for not explaining clearly. If int2 is 0, then the answer should be 123, not 1230. In actuality though, int1 (the number on the left side) would only have a value if int2 goes over the 32 bit limit. So when int2 is 0, then int1 is 0 (or garbage, i'm not sure).

+7  A: 

You could convert them into strings, combine them and then convert them back to an int?

Buckley
Is this the only way to do it? If so, what's the code?
Bei337
A: 

You can turn them into strings and concat them.

itoa(num, buf, 10)

http://www.cplusplus.com/reference/clibrary/cstdlib/itoa/

george9170
itoa() is non-standard, so I wouldn't recommend using it.
Maulrus
using sprintf in stead may be advisable.
NomeN
Meh, if itoa doesn't exist you can write it in about 30 seconds...
Brian Postow
+3  A: 

You could use stringstream:

string Append(int _1, int _2){
    stringstream converter;

    converter << _1 << _2;

    return converter.str();
}

then call atoi on the returned string.

wheaties
Or use `>>`....
Potatoswatter
Could you explain a little on stringstream? I'm not familiar with this.
Bei337
@Bei337: See http://www.cplusplus.com/reference/iostream/stringstream/
Void
+6  A: 

For each digit in int2, you can multiple int1 by 10 and then add int2:

// merge(123, 0) => 1230
int merge(int int1, int int2)
{
    int int2_copy = int2;
    do
    {
        int1 *= 10;
        int2_copy /= 10;
    } while (int2_copy);

    return int1 + int2;
}

You could get rid of the loop using log10 and ceil.

R Samuel Klatchko
Sorry - I wasn't trying to copy you. You just beat me to the punch.
csj
Return type should be something 64 bit (__int64 for MSVC, long long for gcc, might also depend on platform). Also int1 as used in the loop should be 64 bit. Ah, and "int" is not necessarily 32 bit either, so again platform specific type needed :).
Eugene
This function looks clock cycle hungry. Would the method posted below by Buckley work better? If so, can anyone show the code? If not, why is the merge() way better\more efficient?
Bei337
@Bei337 I think converting to string actually requires a loop of divisions by 10 to extract the characters. I would bet money that this is less cpu intensive than Buckley's suggestion.
csj
+6  A: 

Assuming both ints are non-negative, and int1 goes on the left and int2 goes on the right, you need to figure out how many digits long int2 is, multiply int1 by 10 a bunch of times, and then add them.

unsigned int int1 = blah;
unsigned int int2 = blah;

unsigned int temp = int2;

do
{
    temp /= 10;
    int1 *= 10;
} while (temp >0)

unsigned int newInt = int1 + int2;
csj
+1 - excellent use of `unsigned`!
p.campbell
Great approach CSJ. Did you copy from the guy above (Samuel).
Germ
@Germ I wasn't trying to copy - Samual was faster at getting it up.
csj
@Germ: This is the common solution, I suspect many people were writing it. I was writing the same thing as Samuel (though I was using log10 and ceil), but stopped when I saw he posted it. csj just happened to be a bit faster than me.
GMan
CSJ - I didn't know you two were so intimate
Germ
GMan - I was just giving him a hard time
Germ
doesn't handle int2=0 correctly, see question comments
Ben Voigt
@Ben Good Call. It should be fixed now.
csj
@Germ Nice Trolling. Keep up the good work.
csj
+11  A: 

The power of ten, that you need to multiply the first number with, is the smallest one, that is bigger than the second number:

int combine(int a, int b) {
   int times = 1;
   while (times <= b)
      times *= 10;
   return a*times + b;
} 
sth
except times starts at 10, to handle the case when b = 0, see the question comments. AFAICT that fix makes this approach the only non-string method that yields the correct answer. Plus multiplication is faster than division, this replaces a bunch of divisions with a single multiply.
Ben Voigt
I like this answer the best so far, but it would be better if you used log10 instead of the loop. Also, you might want to make the return type a 64-bit int.
rmeador
@sth All you need to do is change the while to a do-while, and b=0 is accounted for. Best Answer!
csj
@csj I think this solves my question. I mistook the b=0 scenario (read my edit in question). Thank you all :)
Bei337
A: 

Another option that works for C too:

#include <stdio.h>

int CombineInt(int int1, int int2)
{
  char cResult[32];

  sprintf(cResult, "%d%d", int1, int2);
  return atoi(cResult);
}
Juliano
+1  A: 

The following is essentially sth's accepted solution but with the b==0 fix, and the loop replaced with an expression to calculate the scale directly:

#include <math.h>

int combine(int a, int b) 
{
    int times = 1;
    if( b != 0 )
    {
        times = (int)pow(10.0, (double)((int)log10((double)b)) + 1.0);
    }
    return a * times + b ;
}

In some circumstances (such as a target with an FPU and a good maths library) the expression might be faster than the loop, but I have not tested that hypothesis.

Clifford