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

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.