# Thread: dividing a multi-digit into single-digits

1. ## dividing a multi-digit into single-digits

i am trying to figure out how to make a multi-digit number be broken down into single-digits.

For example, if a=534, how can i make a program that takes that variable and makes the following information true:

a=534
b=5
c=3
d=4

i need a line of code that can break it down, if that is possible

thanks..........

2. I think you can do:

string number = 123;
string a = number[0];
string b = number[1];
string c = number[2];

3. Assigning an integer to a string doesn't work in C++ - at least not in my MS Visual Studio .Net using VC7.

There are probably three dozen different ways to do this, but one is as h3ro suggests, to convert the number to a string (sstream may help with this), for example.

Another way is to use modulo and divide by ten a number of times (until there's "nothing left"),

--
Mats

4. I would suggest h3ro's way. It seems to be the easiest.

5. But don't you need data declared as a string to go by h3ro's way?

6. Yes, you can do that with a stringstream. However, I'd go with modulo and division.

1234 &#37; 10 = 4
1234 / 10 = 123
123 % 10 = 3
123 / 10 = 12
...

7. Originally Posted by Daved
Yes, you can do that with a stringstream. However, I'd go with modulo and division.

1234 % 10 = 4
1234 / 10 = 123
123 % 10 = 3
123 / 10 = 12
...
Wouldn't this also be alot faster than using a std::string and stringstreams?

8. I highly doubt speed would matter in this case. Clear and correct code would be more important things to aim for.

It is also quite possible that this is an assignment of some sort, and my guess would be that the math way would be what was intended rather than the string way, if it matters.

9. Originally Posted by Neo1
Wouldn't this also be alot faster than using a std::string and stringstreams?
The by far most calculation intensive part of splitting a number into a string is the divide instruction - and if the compiler isn't VERY clever, you'll get two of them, one for the modulo and one for the divide. You can do MANY regular instructions for one divide operation (on a AMD K8 processor, this is still a 12-cycle operation - on a 68000 processor of old, it would take around 150 clock-cycles to do a divide, and the result would only be 16 bits too!). 12 cycles can do 36 or so normal single-cycle instructions on a K8 (which is most of the other instructions in the x86 instruction set).

--
Mats

10. div() and ldiv() from <cstdlib> can be used to explicitly compute the quotient and remainder simultaneously (at least if the processor is capable of it, anyway).

11. Originally Posted by robatino
div() and ldiv() from <cstdlib> can be used to explicitly compute the quotient and remainder simultaneously (at least if the processor is capable of it, anyway).
Good point. I'm not aware of any integer divide-capable processor that doesn't also give the remainder as a side-effect - it is sort of trivial to calculate that when you do the divide - otherwise one would have to implement a separate instruction "mod" that does that, and it's not any more efficient. The same applies, by the way, to the more common versions of integer divide algorithms used on processors without hardware for divide.

I tested with Visual Studio .Net, and in my test, the compiler did only one div instruction for both the divide and the modulo at once. I believe GCC does the same, which covers most code produced... :-)

--
Mats

12. Originally Posted by matsp
and if the compiler isn't VERY clever, you'll get two of them, one for the modulo and one for the divide.
It doesn't need to be that clever, really. It's not hard to scan the intermediate code and notice that a particular subexpression is being subjected to both modulo and division. Even in non-optimizing mode, if there are no intervening instructions between the div and mod, the compiler should be able to figure it out.

13. Originally Posted by robatino
div() and ldiv() from <cstdlib> can be used to explicitly compute the quotient and remainder simultaneously (at least if the processor is capable of it, anyway).
There is the downside that there are versions of this function available only for certain types - div(), ldiv(), lldiv(), imaxdiv() for int, long, long long, and intmax_t, resp. (only the first two being standard C++). This means that if one wants to compute i/j and i%j where i and j are std::size_t, say, it might be better to just use i/j and i%j in close proximity and hope that it gets optimized. Is there a reason why there isn't a templated version in C++, similar to the way that functions from <cmath> such as sqrt() are templated in C++ for different floating-point types, even though in C they're only available for double?

14. If you go the string-way you could also use boost:lexical_cast<string>(int) instead of stringstream. I find it more convinient