954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Function to make a variable equal to other variables

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

salman213
Light Poster
32 posts since Jun 2008
Reputation Points: 10
Solved Threads: 0
 

int c = (a * 10) + b;

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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.

salman213
Light Poster
32 posts since Jun 2008
Reputation Points: 10
Solved Threads: 0
 

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;

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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.

Alex Edwards
Posting Shark
972 posts since Jun 2008
Reputation Points: 392
Solved Threads: 109
 

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.

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

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 :)

salman213
Light Poster
32 posts since Jun 2008
Reputation Points: 10
Solved Threads: 0
 
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. :)

VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
 
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

Alex Edwards
Posting Shark
972 posts since Jun 2008
Reputation Points: 392
Solved Threads: 109
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You