0

I am trying to store multiple integers in one int variable. So like this:

int num1 = 2;
int num2 = 6;
int num3 = num1 + num2;

But I get 8. I want 26. I am aware this is the result I should expect, but I would just like a method of tying the integers together instead of adding them and I couldn't think of a better example.

Thank you!

Jerry Stratton
  • 3,287
  • 1
  • 22
  • 30
Toxxic
  • 57
  • 1
  • 1
  • 6
  • you're not even wrong – Isaac Jul 21 '16 at 00:53
  • Why are you trying to store multiple values in a single int? Is this some code-golfing exercise or are you simply insane? Clearly it's impossible to store multiple *int* values in an *int* because I can't store the maximum int value into an int more than once. – Olipro Jul 21 '16 at 00:55
  • 1
    _@Toxxic_ How generic do you need this? Could `num1` and `num2` actually exceed `9`? – πάντα ῥεῖ Jul 21 '16 at 01:26
  • Are you sure you're not looking for an *array*? – user253751 Jul 21 '16 at 01:27
  • @immibis eeeeextrapolation! – πάντα ῥεῖ Jul 21 '16 at 01:27
  • 4
    You don't concatenate integers because there is no such thing as "integer concatenation". You don't add texts because there is no such thing as "text addition". You don't put orange juice and water into a single glass because they won't stay like "orange juice and water", and they will turn into "dilute orange juice". Just... don't lose common sense. –  Jul 21 '16 at 01:36

7 Answers7

6

You can convert them to string, add them, and convert them back (which isn't efficient as the second option)

int num3 = std::stoi(std::to_string(num1) + std::to_string(num2));

or a more "mathy" way (which only works if num2 is only a digit)

int num3 = num1 * 10 + num2;

Note that the first solution works for any numbers, which also means that without storing the length of the number there is no way to get the original numbers back.

Rakete1111
  • 47,013
  • 16
  • 123
  • 162
  • I had the exact same answer typed out when I saw yours appear :) – Bas in het Veld Jul 21 '16 at 00:56
  • I wouldn't use `std::pow` for this. A for loop multiplying by ten is going to be much, much faster for any number of iterations that will fit in an `int`. Also allowing arbitrary lengths will result in lost information. You wouldn't know where to split to take num3 apart again. – user4581301 Jul 21 '16 at 01:06
  • Do you need to include anything here (or something similar)? Because I have the following error: namespace "std" has no member "to_string". Also, namespace "std" has no member "stoi" – Toxxic Jul 21 '16 at 01:06
  • @Toxxic yes, `` – Rakete1111 Jul 21 '16 at 01:07
  • @user4581301, Is that little bit of speed worth sacrificing the code readability by putting a loop in place of a well-named function? Probably not, and same for writing a `pow` that is as fast as that loop. – chris Jul 21 '16 at 01:10
  • @Rakete1111 Remove all these silly string processing parts, and I'll upvote your answer. – πάντα ῥεῖ Jul 21 '16 at 01:14
  • @Rakete1111 _"Why is it silly?"_ Because it's not necessary and overly costly compared to simple math. – πάντα ῥεῖ Jul 21 '16 at 01:36
  • @πάνταῥεῖ True, but if performance isn't a factor, it is better than calculating the numbers's length and using math – Rakete1111 Jul 21 '16 at 01:37
  • @Rakete1111 I'm pretty sure a _"mathy"_ way could be found for numbers given beyond `9`. – πάντα ῥεῖ Jul 21 '16 at 01:39
  • @Rakete1111 Upvoted finally. Though having the _pure math_ solution would be nice. I don't have it at hand right now, and I'm a bit too tired to look up for it right now. – πάντα ῥεῖ Jul 21 '16 at 01:52
0

Well, what about just adding the digits multiplied and pushed to their intended positions?

int num3 = num1 * 10 + num2;
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
0

I think you want to concatenate the two string literals.
Convert the int to string and then concatenate the two strings This will give you the concatenated string "26" from the strings "2" and "6" If you need the int 26 then you can convert it back to an int after the concatenation. Good luck :-)

Community
  • 1
  • 1
Rick D
  • 26
  • 2
0

You can store multiple integers as one integers using bitwise operation.

For the example you provided:

int num1 = 2; // 0010
int num2 = 6; // 0110

You can then concatenate the numbers using 4 bits:

int num3 = (2 << 4) | 6; // 00100110 num3 = 38

Or if you are using 3 bits, then it becomes:

int num3 = (2 << 3) | 6; //010110 num3 = 22

To retrieve back the numbers, you will have to do a Right shift on num3 to get num1 and do a bitwise AND with 2 to the power of number of bits - 1 (2 ^ n - 1, where n = number of bits).

For 4 bits, num3 = 38;

num1 = num3 >> 4; // 2
num2 = num3 & (2 * 2 * 2 * 2 - 1); // 6

For 3 bits, num3 = 22:

num1 = num3 >> 3; // 2
num2 = num3 & (2 * 2 * 2 * 2 - 1); // 6

Hope this helps.

Intotito
  • 1
  • 2
0

Little bit late, but:

You could do make a string of those two integers and then convert it back to a string.

Here's my more simple but bigger code:

int foo = 2;
int bar = 6;
string temp = to_string(foo) + to_string(bar);
int foobar = stoi(temp);
cout << foobar;

And here's my smaller one:

int foo = 6;
int bar = 2;
int foobar = stoi(string(to_string(foo)) + string(to_string(bar)));
cout << foobar;

Maybe not as efficient as the other examples but it also works if there are two-digit numbers.

Tobias
  • 1
  • 3
  • That is exactly what [Rakete1111 proposed](https://stackoverflow.com/a/38493151/4850040), 6½ years ago. There's no additional value in this answer. – Toby Speight Dec 12 '22 at 15:13
0

This seems like an old question and I've stumbled that I want to have this capability also.

Because nobody yet mentioned this, if you would have threshold that would validate that the number is below that range ceiling what you can do is store both numbers in one number type by storing their bits separately.

You would store the first number setting the bits as you would in little endian, for the second number you would store the bits in big endian.

Then you can abstract this to working just with bytes as size and the length of a byte depending on your arch, usually 1 byte is 8 bits so that means using lets say uint16 (2 bytes) we can restrict each number to half the space as in each number stored would need to be a uint8.

If we store in a uint8 then both numbers should be represented within 4 bits each.

This would be useful for small numbers because of the obvious restrction..

// Examples
// uint8 - store first number - little endian
[0][0][0][0] [1][1][1][1] = 15 // storing in 4 bits the max value
// uint8 adding value of 3 as second value in big endian
[1][1][0][0] [1][1][1][1]
// uint16 store first number - little endian
[0][0][0][0][0][0][0][0] [1][1][1][1][1][1][1][1] = 255 // storing in 8 bits the max value

so max value is 15 if you store in 4 bits and 255 if you would store in 8 bits, anything stored above that would overflow and corrupt the other stored value.


Obviously treating them as little endian and big endian only works with two numbers when you don't need further partitioning as it helps when thinking about how they are stored. And there might be libraries that automatically help between endianness representation.

But you could partition the bytes in how many slices of bits you want it's just that without using endianness you might need to do more conversion manually.

Partitioning is also in the answer of intotito yet few answers point out that you need a ceiling threshold for your values, else it's just a loaded footgun.

Mihai
  • 82
  • 1
  • 6
-1

You would have to store them as strings to concatenate them like that as so:

string str_num1 = to_string(num1);
string str_num2 = to_string(num2);
string result = num1 + num2;

This is a bad way to do it though because if you use any number that's not 1 digit, it will get way too complicated to store and/or pull out the data.

Use a vector or pair. Much easier and less hassle:

pair<int, int> presult = make_pair(num1, num2);  ///or
vector<int> vresult;
vresult.push_back(num1);
vresult.push_back(num2);  ///num1 in spot 0, num2 in spot 1

vector should be best since it can hold arbitrarily large number of ints

Dylan
  • 92
  • 4
  • Way to complicated. No strings necessary at all. – πάντα ῥεῖ Jul 21 '16 at 00:58
  • Yah, strings are too complex, that's why vector or pair is much better – Dylan Jul 21 '16 at 01:01
  • Well, compare your answer to mine? Why do you need all that fancy STL stuff actually? – πάντα ῥεῖ Jul 21 '16 at 01:02
  • SLT is fancy? It's practically built into C++ – Dylan Jul 21 '16 at 01:03
  • 1st note it's STL, not SLT 2nd note the STL isn't the c++ standard library but something else 3rd what's actually wrong just doing a simple piece of math operation instead of complicated string constructing operations where you don't need them? **Use the right tools for solving simple things** – πάντα ῥεῖ Jul 21 '16 at 01:05
  • I didn't mean to come off as aggressive, but I still using multiplication is a poor solution. What is the numbers were 11 and 25? What if you had more than two numbers? The OP said they were trying to find a way to store ints together (or tying them together as they say). Using strings solves the problem exactly in all cases and vector solution solves the problem of holding the data, which I imagine the intent of the question was. I'm just confused why you would dismiss STL (sorry for typo) when it's easy and the solution gets at the idea of the question.... – Dylan Jul 21 '16 at 01:15
  • Clarify all of these points in your answer please, I prefer going KISS regarding the given information. _Over engineering_ costs industry billions of dollars yearly. – πάντα ῥεῖ Jul 21 '16 at 01:19