very simple question: what is the most easiest way to do the following

I have two variables

a=1;
b=2;

how do I make it that

c= a,b

so that c=12

I dont want to write

c=12;

because a and b change according to user input.

Thanks, this should be solved fast :) I dont really get what I should "search" for otherwise I would search instead of asking. (also the title is probably pretty bad but not sure what to call this :S)


Thanks

Dani AI

Generated

Shortly: there are two practical, commonly used ways to get c to read like the digits of a followed by the digits of b — an arithmetic (digit-scaling) method and a string-concatenation method. ’s approach in the thread is the arithmetic idea; ’s point about the shift trick is worth noting — bit shifts only help for powers of two and make intent harder to read.

String-based (simple, preserves leading zeros, easy to get right):

// C++11+
std::string s = std::to_string(a) + std::to_string(b);
int c = std::stoi(s); // stoi can throw on bad input

Use std::ostringstream or manual parsing if you’re on an older compiler. Keep input as strings if you care about leading zeros (e.g., wanting "102" from a=1, b=02).

Numeric (no allocations, faster for pure numbers; handles multi-digit b):

int concat(int a, int b) {
    int tb = (b < 0) ? -b : b;
    int mul = 1;
    if (tb == 0) mul = 10;
    while (tb > 0) { mul *= 10; tb /= 10; }
    return a * mul + b;
}

This computes the proper decimal multiplier (10^digits(b)) then adds b. Use a wider integer type or checks for overflow if a and b can be large.

Gotchas and when to choose which: string concat is easiest and preserves formatting; numeric is slightly faster and yields a true number but loses leading zeros and must guard against overflow and negative values. For one-digit b the scaling is trivial, but for general inputs use the multiplier loop above or the string method.

Recommended Answers

All 8 Replies

int c = (a * 10) + b;

Hmm I guess that is the way to do it if you want to go mathematically .. I should have thought of that...:( so dumb..lol

Thanks Ancient :)


I will use that but just to check if anyone knows of another way to do it please do post.

What I posted is also the method used to convert a string of digits to an integer if you don't want to use a standard C or C++ convertion function.

Here's aother way: int c = (a << 3) + (a << 1) + b;

What I posted is also the method used to convert a string of digits to an integer if you don't want to use a standard C or C++ convertion function.

Here's aother way: int c = (a << 3) + (a << 1) + b;

Edit: Wrong on my part, sorry.

Edit2: Actually... I think I got it.

a << 3 is the same as a * (2 ^ 3)
a >> 3 is the same as a / (2 ^ 3)

Correct me if I'm wrong on this.

Edit: Wrong on my part, sorry.

Edit2: Actually... I think I got it.

a << 3 is the same as a * (2 ^ 3)
a >> 3 is the same as a / (2 ^ 3)

Correct me if I'm wrong on this.

Point made: it's obfuscatory and should only be used in the archaic systems, from whence it came, that lack the appropriate machine commands and/or a supporting assembly code library.

which way is "obfuscatory?"

Is there a specific command that has not been mentioned yet which is now available that will do the same thing?


Not that I have any problem with the way described :)

which way is "obfuscatory?"

Well, you are given two ways of doing the same thing:

int c = (a * 10) + b;
int c = (a << 3) + (a << 1) + b;

One of them is straight-forward, one is not. The one that is not is "obfuscatory" (i.e. more complicated and harder to understand for no reason other than to add confusion).

Is there a specific command that has not been mentioned yet which is now available that will do the same thing?

You're missing the point. When AD posted this:

int c = (a << 3) + (a << 1) + b;

his point was along the lines of "I've given you a perfectly good solution, yet you ask for another one with no explanation of why the first solution doesn't meet your needs." Then he gave you a solution that does the exact same thing as the first solution, but in an overly-complicated way that no one would ever use instead of the first solution. Why don't you like this?

int c = (a * 10) + b;

I can't think of a command that will do this any better.

Not that I have any problem with the way described :)

Why are you asking for a different way then? I'm sure we could come up with even more confusing mathematical formulas that do the exact same thing, but that's probably not what you are looking for. :)

commented: Much better said than me. +14

Point made: it's obfuscatory and should only be used in the archaic systems, from whence it came, that lack the appropriate machine commands and/or a supporting assembly code library.

I laughed so hard when I read this...

winner

commented: I actually have encountered it in the wild for this very reason, as I imagine AD has. +14
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.