# combine two variables

if I have 2 variables and I want to combine as if it were a dozen and the unit . How can I?

es:

``````int x=5;
int y=5;
int xy;
void setup() {
Serial.begin(9600);
}

void loop() {
Serial.println(xy);
}
``````

I would that the variable xy was equal to 55

``````int x=5;
int y=5;
int xy;
void setup() {
Serial.begin(9600);
}

void loop() {
Serial.print(x);
Serial.println(y);
}
``````

This isn't combining its just printing them out next to each other

If you want a string that has the 2 numbers next to each other, there are various approaches, but as I'm lazy so I'd use sprintf

I would that the variable xy was equal to 55

Then use:-

``````xy = x * 10 + y
``````

Hi Mic

I'm not sure that works if the numbers are greater than nine

Best to convert them to strings and then concatenate

But I guess its not entirely clear what they really want to achieve or why

Enricothreewesa:
if I have 2 variables and I want to combine as if it were a dozen and the unit . How can I?

Grump_Mike's post shows how to do it, but you need to make up your mind whether you're dealing with tens and units or dozens and units.

xy = x * 10 + y

dozens and units is base 12 so
xy = x * 12 + y
May have to code a base 12 routine if the numbers are larger.

steinie44:

xy = x * 10 + y

dozens and units is base 12 so
xy = x * 12 + y

Agreed
but if they were dozens and units the x= 5 and y =5 would not give you a value of 55 for a combination of the two, it would give you 65.
So the OP needs to say a bit more precisely what he wants to do.

Mike
I'd not noticed the reference to dozen.

Mainly because of the last part where the OP said that XY = 55

Which doesn't make sense if they wanted to use base 12. Ie even if they mean x * y. It would be 21 base 12.

As already pointed out, the question makes no sense. x = 0101 and y = 0101 (in binary) and there is no way to "combine" them into 55. Someone already pointed out that the correct value for 5 dozen+5 is 65.

Note that C++ makes it easy to do something like this. First, you define a class, e.g.,

class ByTheDozen {
int dozens;
int units;
public:
ByTheDozen(int doz, int un) { dozens = doz += un / 12; units = un % 12; }
ByTheDozen(int units) { dozens = units / 12; units = units % 12; }
ByTheDozen & operator +(const ByTheDozen & value) { dozens += value.dozens;
dozens += (units + value.units) / 12;
units = (units + value.units) % 12; return *this; }
ByTheDozen & operator +(int value) { dozens += (value / 12);
dozens += (units + value) / 12;
units = (units + value) % 12;}
int operator() { return dozens * 12 + units; }
bool operator =(const ByTheDozen & a, const ByTheDozen & b) { return (a.dozens == b.dozens) && (a.units == b.units); }
ByTheDozen & operator=(const ByTheDozen & value) { dozens = value.dozens; units = value.units; return *this; }
ByTheDozen & operator++() {units = (units + 1) % 12; dozens = (dozens + (units + 1) / 12); return *this; } // I think this is ++prefix
// ...and so on .
};

There are more compact ways to express operator +, but I expanded it for clarity. I'm doing a lot of this from memory, and it's been a while since I defined an arithmetic class--like about two years, so standard disclaimer: this is a suggestion which may or may not compile; the user is expected to consult the C++ reference materials for precise details. For example, I always have to look up how to define the postfix ++ and -- operators, but they all involve computations like

ByTheDozen & forgotten syntax here { ByTheDozen t = *this; (*this)++; return t; }

Strictly speaking, the definition of operator= is not required, because the default value-copy constructor works, but I like to be thorough. You have to add operator - for both ByTheDozen and integer arguments, ++, --, etc.

Like most such questions, the OP asks "how do I do THIS" without stating the purpose, which makes giving a coherent answer all but impossible. But I wouldn't waste time trying to "combine" variables (which is a silly way to state the problem, since it gives no idea what the purpose is); instead, I'd build the general ByTheDozen class. Note this generalizes to ByTheGross.

Note that dealing with negative values requires some care.

Note also that the dozens and units are protected variables, meaning they cannot be changed or set by the programmer except through the interface that guarantees that 0<=units<12.

Given the above definitions, I can write
ByTheDozen x(5, 5);
ByTheDozen y(65);
x += 24;
y += 24;
println(x == y); // "true"
println((int) y); // "89"

More can be learned from any good C++ reference, e.g., Stroustrup 3rd edition.
joe